views:

241

answers:

5

Is there any way to supress 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 any way to suppress this behavior?

A: 

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>\"'
zdan
A: 

Escape the inner quotes like this:

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

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...

nablaodel
A: 

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
Yanagi
A: 

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

Chris Towles