VSTS release, CMD webdeploy with variables inside Powershell quotation syntax - powershell

Apologies in advance if this is dumb and obvious. I have the following lines in a Powershell script executing as a release task in VSTS:
Write-Output "Stopping IIS..."
CMD.exe /C "'C:\Program Files\IIS\Microsoft Web Deploy V3\msdeploy.exe' -verb:sync -source:recycleApp -dest:recycleApp='MyApp/MyAppPool',recycleMode='StopAppPool',computername=$privateIp,userName=$serverLogin,password=$serverPassword"
It produces the following output:
2017-10-07T15:07:33.3282985Z Stopping IIS...
2017-10-07T15:07:34.5470804Z ##[error]The filename, directory name, or volume label syntax is incorrect
Per MS (https://technet.microsoft.com/en-us/library/cc956689.aspx) this means one of the following characters is wrong: \ \ / [ ] : | < > + ; = . ? "
I can only assume it's my quote syntax because that's the only character used on this line. I know it's not the contents of any of the variables, because they are used elsewhere in the script with no problem (and don't contain any of the characters in question).
Help?

After a lot of trial and error, I figured out the answer:
CMD.exe /C "`"C:\Program Files\IIS\Microsoft Web Deploy V3\msdeploy.exe`" -verb:sync -source:recycleApp -dest:recycleApp='MySite/MySiteAppPool',recycleMode='StopAppPool',computername=$privateIp,userName=$serverLogin,password=$serverPassword"
Just a note for posterity, this is specifically a syntax that will work when you are attempting to stop/start/recycle an app pool on a server on the network, from a Powershell script, as a release task in VSTS, using msdeploy.

Related

Jenkins parameter Release versus Staging

I created a freestyle job in Jenkins that I just set up (latest version).
I added parameters to it. One of those is a Options selection for ReleaseType with the options of Staging and Release.
One of the build steps is executing a remote command on the server when the site is uploaded to. It uses the Execute Windows Batch Command build step.
Here is the command line (with things made generic):
sexec myuser#mysite.com -pw=mypassword -cmd="PowerShell -Command ""C:\batch\bvCopyFast.ps1 C:\inetpub\mysite${ReleaseType}\siteLoad C:\inetpub\mysite${ReleaseType}\site""
Basically I am executing a powershell command that uses Robocopy to copy the files from the upload folder to the actual release folder for the site.
As you can see I need to have the ${ReleaseType} replaced with the actual value. The problem is that when this gets executed it isn't doing the substitution. I just uses that literal value in the command and that doesn't work.
If you use the -Command parameter it implies you are going to write raw PowerShell code in between the quotation marks that follow (allow you can call a script as you have).
PowerShell -Command "Get-Date; pause;"
To call a PowerShell script file you should use:
PowerShell -File "Your-Script.ps1 -Parameter1 Argument1 -Parameter2 Argument2"
https://learn.microsoft.com/en-us/powershell/scripting/components/console/powershell.exe-command-line-help?view=powershell-6
I would write a PowerShell script that accepted your root path and the releaseType as arguments and execute that.
Param($rootPath,$releaseType)
{
robocopy "$($rootPath)\$($releaseType)\siteLoad" "$($rootPath)\$($releaseType)\site"
}
I have never used Jenkins so I hope this works as I expect it to!
sexec myuser#mysite.com -pw=mypassword -cmd=""PowerShell -File 'C:\batch\newScript.ps1' -RootPath 'c:\inetpub\mysite' -ReleaseType {ReleaseType}""

How to run iis backup executable in powershell

I am trying to run the following command and have tried passing arguments and hash tables but it did not work:
msdeploy.exe -verb:sync -source:metakey=lm/w3svc/1 -dest:package=C:\iis_backups\site1.zip,encryptPassword=xyxyz > C:\iis_backups\site1.log
This has been answered before here: Running msdeploy.exe from within Powershell
It appears the solution for your scenario is as follows:
msdeploy.exe '-verb:sync' '-source:metakey=lm/w3svc/1' '-dest:package=C:\iis_backups\site1.zip,encryptPassword=xyxyz' > C:\iis_backups\site1.log

check_nrpe put $ at end of check string

i've a problem with my powershell nagios script, mounted on a MS Windows Server 2008 64bit, with a NRPE_NT daemon.
I've declared the command like this:
command[check_files]=cmd /c echo C:\nrpe\libexec\check_file.ps1 $ARG1$; exit($lastexitcode) | powershell.exe -command -
I've set ExecutionPolicy as unrestricted
I've restart NRPE_NT Services and declared command on console, like this:
$USER1$/check_nrpe -H $HOSTADDRESS$ -t 60 -c check_files -a $ARG1$
Now, why if i run it locally, it works good:
C:\>cmd /c echo C:\nrpe\libexec\_file.ps1 C:\nrpe; exit($lastexitcode)| powershell.exe -command -
No file/s present with this string
But if i run it by check_nrpe i receive this output:
'-' was specified with the -Command parameter: no other arguments to -Command are permitted.
In Debug mode, on NRPE.log i can see this:
Running command: cmd /c echo C:\nrpe\libexec\check_file.ps1 C:\nrpe;
exit($lastexitcode) | powershell.exe -command - $
Command completed with return code 0
Why in this way, check_nrpe add a dollar character ($) at end string, derailing the entire control ?
Thanks in advance
I'm not sure if this will help your situation, but I just figured out something in my environment that was causing something similar. Here was my NRPE command comfiguration:
command[foo]=grep file '^pattern$'
Everything was fine until I wanted to add another parameter after the '^pattern$' parameter... that new parameter (at the end of the command-line) would get an extra $ appended to the end.
It seems that NRPE requires $ to be escaped, otherwise it thinks that it's a variable reference and does odd things with it. I was expecting that the quoting would make it so that no escaping was required, but NRPE's configuration files don't follow shell-style quoting rules. So, changing my NRPE configuration to this solved my problem:
command[foo]=grep file '^pattern$$'
Note the double $$ in the revised NRPE command definition.

How to pass the sitename in ProjectName.deploy.cmd

I am trying to run the ProjectName.deply.cmd that is generated by MSBuild when the paramter /p:DeployOnBuild=True is passed. One of the argument "ComputerName" is to be passed as https://WebServer01:8172/MSDeploy.axd?SiteName=MySiteName. My command line would be
ProjectName.deploy.cmd /Y /M:https://WebServer01:8172/MSDeploy.axd?Site=MySiteName
-AllowUntrusted /U:DeployUserName /P:Password /A:Basic
It returns
Error: Unrecognized argument 'MySiteName'. All arguments must begin with "-".
the actual command that is executed is
"C:\Program Files\IIS\Microsoft Web Deploy V3\\msdeploy.exe"
-source:package='Y:\ProjectName.zip'
-dest:auto,computerName='https://WebServer01:8172/MSDeploy.axd?Site',userName='DeployUserName',password='Password',authtype='Basic',includeAcls='False'
-verb:sync -disableLink:AppPoolExtension -disableLink:ContentExtension
-disableLink:CertificateExtension
-setParamFile:"Y:\ProjectName.SetParameters.xml"
MySiteName
-AllowUntrusted
Notice that the argument to /M https://WebServer01:8172/MSDeploy.axd?Site=MySiteName is split into two arguments and thus creating computerName='https://WebServer01:8172/MSDeploy.axd?Site' and and extra argument MySiteName.
I have gone through Running a deployment package with quoted parameters fails in Visual Studio 2010 Service Pack 1 but that takes care of only ArgMsDeployAdditionalFlags and not the arguments e.g. /M:ComputerName.
When the SiteName is not passed, I can do the deployment fine with an user that had admin rights on the server but when a standard IIS user DeployUserName is used I get 401
ProjectName.deploy.cmd /Y /M:https://WebServer01:8172/MSDeploy.axd
-AllowUntrusted /U:DeployUserName /P:Password /A:Basic
The server returns 401
Error Code: ERROR_USER_UNAUTHORIZED
More Information: Connected to the remote computer ("WebServer01") using the Web
Management Service, but could not authorize. Make sure that you are using the
correct user name and password, that the site you are connecting to exists, and
that the credentials represent a user who has permissions to access the site.
Learn more at: http://go.microsoft.com/fwlink/?LinkId=221672#ERROR_USER_UNAUTHORIZED.
Error: The remote server returned an error: (401) Unauthorized.
The permissions for that user are fine as the publish from VS2012 with MSDeploy profile using that user works just fine. I can also build msdeploy.exe command and that also runs fine. I have to use the ProjectName.deploy.cmd as it is being produced as part of Team Build from TFS2010.
Have you tried quoting the argument?
ProjectName.deploy.cmd /Y "/M:https://WebServer01:8172/MSDeploy.axd?Site=MySiteName"
-AllowUntrusted /U:DeployUserName /P:Password /A:Basic
Thought I'd add to the answer for anyone else like me that stumbles upon this trying to figure out why this doesn't work, and who'd like the equivalent msdeploy command.
As mentioned in the comments quoting the argument won't work due to a bug in how the arguments are parsed, from MS docs:
At the time of writing, due to a bug in the Web Publishing Pipeline
(WPP), you can't run the .deploy.cmd file using an endpoint address
that includes a query string. In this scenario, you need to deploy
your web package by using MSDeploy.exe directly.
Source: https://learn.microsoft.com/en-us/aspnet/web-forms/overview/deployment/web-deployment-in-the-enterprise/deploying-web-packages
So, instead, you should use MSDeploy directly. Equivalent to the deploy.cmd arguments in the question:
MSDeploy.exe -source:package='<fullPathToDeployable>\<projectName>.zip'
-dest:auto,computerName="https://<ipOrDnsName>:8172/MSDeploy.axd",userName="<userName>",password="<pwd>",authtype="Basic",includeAcls="False"
-verb:sync
-disableLink:AppPoolExtension
-disableLink:ContentExtension
-disableLink:CertificateExtension
-setParamFile:"<fullPathToDeployable>\<projectName>.SetParameters.xml"
-setParam:name="IIS Web Application Name",value="<siteName>"
-AllowUntrusted
(replace angle-bracketed words)

MSBuild fails when run via Powershell

I have an MSBuild script that I want to call from a PowerShell script as part of a deployment process. If I call the build script via a bat file all works well. If I do the exact same thing in PowerShell I get CS1668 error looking for wierd and wonderful paths that dont exist on my machine.
I know I am getting into the MSBuild script as these errors are occuring from within the MSBuild script targets (logging output is showing this).
The bat file and the PowerShell script reside in the same place, right next to the build script.
Errors:
error CS1668 : Warning as error :
Invalid search path 'C:\Program
Files\Microsoft Visual Studio
8\VC\AtlMfc\Lib' specified in 'LIB
environment variable' -- 'The system
cannot find the path specified. '
error CS1668 : Warning as error :
Invalid search path 'C:\Program
Files\Microsoft Visual Studio
8\VC\PlatformSDK\Lib' specified in
'LIB environment variable'-- 'The
system cannot find the path specified.
'
I have checked and neiter of these paths do NOT exist on my machine.
Why would running from PowerShell change what paths MSBuild look for?
Thanks in advance
RhysC
EDIT- adding in code: NB the name of the MSBuild script is AutomatedDebug.build
POWERSHELL SCRIPT:
#Begining of script
$CurrentPath = Split-Path (Split-Path $myinvocation.mycommand.path )
#Assign the Build script paths. 1 is for building and testsing, 1 is for deployment (to keep things clean)
$MSBuildScriptBuildAndTestPath = $CurrentPath + "\Tools\AutomatedDebug.build"
$MSBuildScriptDeployPath = $CurrentPath + "\Tools\Deploy.build"
#Run the automated build with tests
Write-Host "*** Run the automated build with tests ***"
C:\Windows\Microsoft.NET\Framework\v3.5\MSbuild.exe $MSBuildScriptBuildAndTestPath "/t:AllTests" "/l:FileLogger,Microsoft.Build.Engine;logfile=AllTests.log"
if($LastExitCode -ne 0)
{
throw "AllTests failed"
}
Write-Host "*** FINISHED: Run the automated build with tests ***"
Bat File
#C:\Windows\Microsoft.NET\Framework\v3.5\MSbuild.exe AutomatedDebug.build /t:AllTests /l:FileLogger,Microsoft.Build.Engine;logfile="AllTests.log"
#pause
enter code here
Ok, what is actually happening is that I have dowloaded the Powershell Community Extensions (http://pscx.codeplex.com) many moons ago and there is a Environment.VisualStudio2005.ps1 file in there that adds EnvVars that dont exist. This is (I assume) becuase i dont use VS2005. I have commmented this out and all is well. I have tried to look for the 2008 equivilent but dint have too much luck. To be honest i dont use powershell to its full potential so i doubt i even need the PSCX on my machine anyway.
Thanks guys for your help.
It is most likely due to how you are passing parameters into the MSBuild script. There are some key parameter parsing differences between CMD and PowerShell and by using a batch file, you are implicitly using CMD's parameter parsing engine to hand off the parameters to MSBuild. If you post the command line you're using, one of the PowerShell gurus here can probably help debug where the parameters are getting confused.
I am wondering, basically you get "Warning as error" here. You can either try turning off treating warnings as errors (I mean, having a lib path which doesn't exist doesn't sound that bad to me) or you'll let your scripts print out the respective environment variables (using echo %LIB% and $Env:LIB respectively) from the scripts and look for differences.
However, since the envvars for VS aren't set globally by default (that's why there are the three Visual Studio command prompts) you have to set all variables somewhere. If you're doing this within the scripts you may have a typo somewhere?