Reputation: 1707
How do I properly escape the quotes in the -param
value in the following command line?
$cmd="\\server\toto.exe -batch=B -param="sort1;parmtxt='Security ID=1234'""
Invoke-Expression $cmd
This of course fails. I tried to escape the quotes (single and double) using the escape character ` and did various combination, but nothing is working.
Upvotes: 114
Views: 193021
Reputation: 899
It looks like the editor (Microsoft) provided bugged parser. indeed this does not work (escaped double quotes with backtick):
> &sc.exe config LixFax binpath="`"C:\Work\net48\Fax.exe`" 9002"
when this works (bactick escaped with backslash in addition of backtick escaping double quotes, LOL...):
> &sc.exe config LixFax binpath="\`"C:\Work\net48\Fax.exe\`" 9002"
Thus you i manage to set the binpath with doublequotes around the .exe part of service and add additonal parameter after but still part of the binpath parameter of a windows service. And yes in this case the legacy cmd prompt is indeed smarter with simple backslash to escape double quotes!
Upvotes: 0
Reputation: 8650
Escaping parameters like that is usually source of frustration and feels a lot like a time wasted. I see you're on v2 so I would suggest using a technique that Joel "Jaykul" Bennet blogged about a while ago.
Long story short: you just wrap your string with @' ... '@ :
Start-Process \\server\toto.exe @'
-batch=B -param="sort1;parmtxt='Security ID=1234'"
'@
(Mind that I assumed which quotes are needed, and which things you were attempting to escape.) If you want to work with the output, you may want to add the -NoNewWindow
switch.
BTW: this was so important issue that since v3 you can use --%
to stop the PowerShell parser from doing anything with your parameters:
\\server\toto.exe --% -batch=b -param="sort1;paramtxt='Security ID=1234'"
... should work fine there (with the same assumption).
Upvotes: 64
Reputation: 91
In Powershell 5 escaping double quotes can be done by backtick (`). But sometimes you need to provide your double quotes escaped which can be done by backslash + backtick (\`). Eg in this curl call:
C:\Windows\System32\curl.exe -s -k -H "Content-Type: application/json" -XPOST localhost:9200/index_name/inded_type -d"{\`"velocity\`":3.14}"
Upvotes: 5
Reputation: 1381
Using the backtick (`) works fine for me if I put them in the following places:
$cmd="\\server\toto.exe -batch=B -param=`"sort1;parmtxt='Security ID=1234'`""
$cmd
returns as:
\\server\toto.exe -batch=B -param="sort1;parmtxt='Security ID=1234'"
Is that what you were looking for?
The error PowerShell gave me referred to an unexpected token 'sort1', and that's how I determined where to put the backticks.
The @' ... '@ syntax is called a "here string" and will return exactly what is entered. You can also use them to populate variables in the following fashion:
$cmd=@'
"\\server\toto.exe -batch=B -param="sort1;parmtxt='Security ID=1234'""
'@
The opening and closing symbols must be on their own line as shown above.
Upvotes: 115
Reputation: 445
I found myself in a similar predicament today while trying to run a command through a Node.js module:
I was using the PowerShell and trying to run:
command -e 'func($a)'
But with the extra symbols, PowerShell was mangling the arguments. To fix, I back-tick escaped double-quote marks:
command -e `"func($a)`"
Upvotes: 10