How to suppress quotes in PowerShell commands to executables

故事扮演 提交于 2019-12-01 08:21:06

Escape the inner quotes like this:

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]

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
zdan

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>\"'
DGreen

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)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!