Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is up with this PowerShell command line quoting/escaping?

I obviously don't know what I'm doing.

I have finally got this PowerShell command to work. But I can't figure out why it works.

My concern is the final "" characters:

    &"C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.exe" `
    -verb:sync `
    -source:contentPath="$build_directory\deploy" `
    -dest:contentPath="$server_temp_directory,computerName=$server,username=$server_username,password=$server_password" `
    -verbose `
    -postSync=runCommand="powershell -NoLogo -NoProfile -Command $server_temp_directory\remotetasks.ps1 Deploy""

Why do I need double double-quotes?

My IDE (PowerGUI) says the line is not ended correctly, but it is the only way I can make the command run as wanted.

What is it, that I - and the IDE - don't know about quoting in PowerShell?


A little output from echoargs:

If I run:

echoargs -postSync=runCommand="powershell -NoLogo -NoProfile -Command $server_temp_directory\remotetasks.ps1 Deploy""

I get:

Arg 0 is <-postSync=runCommand=powershell -NoLogo -NoProfile -Command \remotetasks.ps1 Deploy>

If I run without the double double-quotes, I get:

Arg 0 is <-postSync=runCommand=powershell>
Arg 1 is <-NoLogo>
Arg 2 is <-NoProfile>
Arg 3 is <-Command>
Arg 4 is <\remotetasks.ps1>
Arg 5 is <Deploy>

Another thing to notice is that the above command does only work if it uses = instead of : in the last argument.

This won't work:

-postSync:runCommand="powershell -NoLogo -NoProfile -Command $server_temp_directory\remotetasks.ps1 Deploy""

I have tried the array solution like this:

$arguments = @("-verb:sync",
               "-source:contentPath=$build_directory\deploy",
               "-dest:contentPath=$server_temp_directory,computerName=$server,username=$server_username,password=$server_password",
               "-verbose",
               "-postSyncOnSuccess:runCommand=powershell -Command $server_temp_directory\remotetasks.ps1 Deploy")
&"C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.exe" $arguments

I still get the same error:

Error: Unrecognized argument '"-postSyncOnSuccess:runCommand=powershell -Command c:\temp\kslog\remotetasks.ps1 Deploy"'. All arguments must begin with "-".

Am I doing some new thing wrong here?

like image 555
asgerhallas Avatar asked Oct 08 '10 11:10

asgerhallas


People also ask

How do you escape quotation marks in PowerShell?

This means that if you hard code a Distinguished Name in PowerShell, and the string is enclosed in double quotes, any embedded double quotes must be escaped first by a backtick "`", and then by a backslash "\".

How do you quote a quote in PowerShell?

You can enclose a string in single quotation marks ( ' ) or double quotation marks ( " ). Quotation marks are also used to create a here-string. A here-string is a single-quoted or double-quoted string in which quotation marks are interpreted literally. A here-string can span multiple lines.

What is Exit command in PowerShell?

| Aug 8, 2022. PowerShell's built-in exit keyword allows you to terminate scripts without closing your PowerShell session. There are different ways to use PowerShell exit to break the execution of a script, function, loop, or switch statement.


1 Answers

This is a notorious issue. The ticket “Executing commands which require quotes and variables is practically impossible” is the most voted bug: https://connect.microsoft.com/PowerShell/Feedback

You can find there a few workarounds as well. But I would recommend you to compose all the parameters as an array and use the & operator to invoke a native command with this array. See the answers and examples: Running an EXE file using PowerShell from a directory with spaces in it and Executing a Command stored in a Variable from Powershell

I did not work with msdeploy.exe and cannot provide some demo code for your case. But I applied this approach to many other tricky native commands well enough. Please, try it and let us know the results.

P.S. Technically this is not exactly an answer to your questions but I assume you are still looking for a practical way of doing this, so it still might be helpful.

like image 137
Roman Kuzmin Avatar answered Oct 30 '22 06:10

Roman Kuzmin