Running msdeploy.exe from within Powershell - powershell

I'm trying to run the following command from within Powershell:
msdeploy -verb:sync -source:archiveDir=c:\KitchenPC\Build -dest:appHostConfig="KitchenPC",computerName=https://192.168.0.3:8172/msdeploy.axd,authType=Basic,userName=someuser,password="secret" -allowUntrusted
The docs say to simply substitute the : after each parameter with an =. So I've tried this:
msdeploy -verb=sync -source=archiveDir=c:\KitchenPC\Build -dest=appHostConfig="KitchenPC",computerName=https://192.168.0.3:8172/msdeploy.axd,authType=Basic,userName=someuser,password="secret" -allowUntrusted
However, I get the error:
Error: Unrecognized argument
'computerName=https://192.168.0.3:8172/msdeploy.axd'. All arguments
must begin with "-". Error count: 1.
I've checked the docs on provider settings, however they have no mention of their equivelent Powershell syntax.

How do you call msdeploy from powershell when the parameters have spaces?
Think this is already answered, just modify it.
Ex. include "KitchenPC" and "secret" using variables, and put the -dest part inside quotation marks.
Working Example:
msdeploy '-verb=sync' '-source=archiveDir=c:\KitchenPC\Build -dest=appHostConfig="KitchenPC",computerName=https://192.168.0.3:8172/msdeploy.axd,authType=Basic,userName=someuser,password="secret"' -allowUntrusted
(Note single quotes around each command line argument)

Related

Invoking a command with variable evaluation in Octopus Deploy Powershell script

I have a simple Powershell script that I execute during an Octopus Deploy installation. This line works fine:
& $exe install --autostart
I runs an application identified by $exe variable with command line arguments "install --autostart".
Now I need to expand command line arguments with a value evaluated from a variable:
& $exe install --autostart -servicename=$serviceName
"$serviceName" is the variable that gets its value during the script execution. Whatever I do it's passed to the line above by variable name, not the value, e.g. it's passed as "$serviceName". I tried single and double quotes, nothing helps. As long it's a command invocation (triggered by the "&" symbol in the beginnging of the line), the rest of the line is interpreted verbatim, no variable substitions.
I used last couple of hours trying to figure this out and this is driving me mad. Any tips are appreciated.
I just did some testing on my side and it looks like if you'd like the variable passed in to the command to be evaluated as a variable it needs whitespace on both sides. So you would want to define your variable as $serviceName = "-servicename=*name*" or if that is not possible then create a new variable just before running the command
$tmpServicename = "-servicename=$($serviceName)"
& $exe install --autostart $tmpServiceName

TFSBuild Powershell step turning script arguments into strings. How to avoid this?

I've configured the following arguments in a Powershell build step: -protocol:http -portsToOpen 9512,9513,9512.
Once TFSBuild runs the whole script, it throws the following error:
"System.Int32[]". Error: "Cannot convert
"9512,9513,9515" to "System.Int32"
The problem is TFSBuild is running the script sorrounding 9512,9513,9515 with quots (i.e. '9512,9513,9515').
Is there any solution for this? One possible workaround would be running powershell.exe from a Command build step... But I'd like to know if there's some direct solution to this issue.
I don't use TFS but I can tell you PowerShell is parsing that as an integer array (System.Int32[]) and trying to pass that as an argument to -portsToOpen
If you don't have variables on that line you could use the stop parsing operator --% as that
directs Windows PowerShell to refrain from interpreting input as Windows PowerShell commands or expressions.
icacls X:\VMS --% /grant Dom\HVAdmin:(CI)(OI)F
You could also actually put it in quotes so that PowerShell just treats it as a string. I could not find the documentation to back up how it expects input for that parameter so I am not sure.

How to write an msdeploy runcommand command that contains strings

Hi I'm trying to write an msdeploy command that contains two strings within the runcommand part of the command.
msdeploy.exe -verb:sync -source:runCommand="^"c:\opensesame\opensesame.cmd^" ^"c:\windows\system32^"" -dest:auto
I was trying to escape the quotes with the carats.
Basically what's suppose to happen is opensesame is called and the path is passed to it so it can open the calculator.
I'm guessing I don't have the right escape character?
If runCommand is not given a full path (and only a full path) to a bat or cmd file, it will execute the command as-is on the server without uploading your bat/cmd file first. If you want to upload the script before executing it, you'll need to bake your parameters into the script and pass it without arguments to runCommand.

How do I call exec in psake to an executable with a variable path?

I can't seem to call this executable correctly in my psake deploy script.
If I do this:
exec { "$ArchiverOutputDir\NServiceBus.Host.exe /install" }
It simply outputs this (and is clearly not calling the executable - just outputting the value of that expression):
c:\ReloDotNet2_ServiceEndpoints\Archiver\NServiceBus.Host.exe /install
But if I do this:
exec { c:\ReloDotNet2_ServiceEndpoints\Archiver\NServiceBus.Host.exe /install }
I get the expected output from the executable.
How do I correctly call an executable with a variable in the path to the executable in psake? If this is actually a PowerShell issue, please feel free to correct the question to reflect that insight.
I
Classic PowerShell issue. Try this instead:
exec { & "$ArchiverOutputDir\NServiceBus.Host.exe" /install }
PowerShell not only executes commands, it also evaluates expressions e.g.:
C:\PS> 2 + 2
4
C:\PS> "hello world"
hello world
What you have given to PowerShell at the beginning of a pipeline is a string expression which it faithfully evaluates and prints to the console. By using the call operator &, you're telling PowerShell that the following thing is either the name of a command (in a string) to be executed or a scriptblock to be executed. Technically you could also use . "some-command-name-or-path". The only difference is that for PowerShell commands, & creates a new scope to execute the command in and . doesn't. For external exes it makes no difference as far as I can tell which one you use but & is typically used.

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)