简体   繁体   中英

How to suppress quotes in PowerShell commands to executables

Is there a way to suppress the enclosing quotation marks around each command-line argument that PowerShell likes to generate and then pass to external executables for command-line arguments that have spaces in them?

Here's the situation:

One way to unpack many installers is a command of the form:

msiexec /a <packagename> /qn TARGETDIR="<path to folder with spaces>"

Trying to execute this from PowerShell has proven quite difficult. PowerShell likes to enclose parameters with spaces in double-quotes. The following lines:

msiexec /a somepackage.msi /qn 'TARGETDIR="c:\some path"'

msiexec /a somepackage.msi /qn $('TARGETDIR="c:\some path"')

$td = '"c:\some path"'

msiexec /a somepackage.msi /qn TARGETDIR=$td

All result in the following command line (as reported by the Win32 GetCommandLine() API):

"msiexec" /a somepackage.msi /qn "TARGETDIR="c:\some path""

This command line:

msiexec /a somepackage.msi TARGETDIR="c:\some path" /qn

results in

"msiexec" /a fooinstaller.msi "TARGETDIR=c:\some path" /qn

It seems that PowerShell likes to enclose the results of expressions meant to represent one argument in quotation marks when passing them to external executables. This works fine for most executables. However, MsiExec is very specific about the quoting rules it wants and won't accept any of the command lines PowerShell generates for paths have have spaces in them.

Is there a way to suppress this behavior?

像这样逃避内部引号:

msiexec /a somepackage.msi TARGETDIR=`"c:\some path`" /qn

Here is a function I use to better handle multiple arguments and those with spaces and quotes. Note that to the code blocks below don't color where strings start and end correctly and you have to use ` to escape quotes you want in the parameter.

function InstallMSIClient{
$Arguments = @()
$Arguments += "/i"
$Arguments += "`"$InstallerFolder\$InstallerVerFolder\Install.msi`""
$Arguments += "RebootYesNo=`"No`""
$Arguments += "REBOOT=`"Suppress`""
$Arguments += "ALLUSERS=`"1`""
$Arguments += "/passive"

Write-Host "Installing $InstallerVerFolder."
Start-Process "msiexec.exe" -ArgumentList $Arguments -Wait }

There's a more complete example on my blog. [ http://www.christowles.com]

Put the entire argument in quotes and escape the inner quotes. Otherwise PowerShell will try to parse it:

msiexec /a <packagename> /qn 'TARGETDIR=\"<path to folder with spaces>\"'

I don't have the answer, but this guy seems to be onto something.
http://www.eggheadcafe.com/software/aspnet/33777311/problem-escaping-command.aspx

I couldn't make it work for me.

Here's someone else reporting the issue too: _http://powershell.com/cs/forums/p/2809/3751.aspx

Here's another idea by someone: _http://www.roelvanlisdonk.nl/?p=1135

That didn't work for me either...

I don't have the answer, but this guy seems to be onto something. http://www.eggheadcafe.com/software/aspnet/33777311/problem-escaping-command.aspx

Yeah, it looks like they found a solution at the end:

Finally worked out how to do this using invoke-expression:

$installprop = "TARGETDIR=" + "```"" + $installpath + "```""

invoke-expression "msiexec /i $packagepath $installprop"

I would recommend using a here-string though to avoid having to do all the escaping.

$command = @'
msiexec /a <packagename> /qn TARGETDIR="<path to folder with spaces>"
'@

invoke-expression $command

I've just hit the problem. The following worked for me:

&cmd /c "msiexec /i `"$appName.msi`" /l* `"$appName.msi.log`" /quiet TARGETDIR=`"D:\Program Files (x86)\$appName\`""

The key was executing cmd rather than msiexec directly. This had two benefits:

  1. I could wrap the entire msiexec command in a string, so it would resolve the the $appName variable while still using the backtick to escape the quotes around TARGETDIR.
  2. I was able to make use of $LastExitCode to check success/failure of the msiexec operation. For some reason $LastExitCode isn't set when calling msiexec directly (hat-tip: http://www.powergui.org/thread.jspa?threadID=13022 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM