Start process with args which contains a path with

2019-02-21 10:21发布

问题:

I need to start a process from a powershell script and pass such params : -a -s f1d:\some directory\with blanks in a path\file.iss to do that, I write the folowing code :

$process = [System.Diagnostics.Process]::Start("$setupFilePath", '-a -s -f1"d:\some directory\with blanks in a path\fileVCCS.iss"') 
$process.WaitForExit()

as a result the process starts but the last argument : -f1d:\some directory\with blanks in a path\file.iss is not passing correctly. Help, please

回答1:

I think you can use Start-Process:

Start-Process -FilePath $setupFilePath -ArgumentList '-a','-s','-f1"d:\some directory\with blanks in a path\fileVCCS.iss"' |
    Wait-Process


回答2:

I understand your question to be: How to pass multiple arguments to start a process where one of the arguments has spaces?

I'm assuming the equivalent in a Windows batch file would be something like:

"%setupFilePath%" -a -s -f1"d:\some directory\with blanks in a path\fileVCCS.iss"

where the double quotes allow the receiving process (setupFilePath in this case) to receive three arguments:

  1. -a
  2. -s
  3. -f1"d:\some directory\with blanks in a path\fileVCCS.iss"

To accomplish this with the code snippet in your question I would use back ticks (to the left of the 1 and below the escape key, not to be confused with a single quote; aka Grave-accent) to escape the inner double quotes like this:

$process = [System.Diagnostics.Process]::Start("$setupFilePath", "-a -s -f1`"d:\some directory\with blanks in a path\fileVCCS.iss`"") 
$process.WaitForExit()

Note that in addition to using back ticks I also changed the single quotes around your argument list to double quotes. This was necessary because single quotes do not allow the escapes we need here (http://ss64.com/ps/syntax-esc.html).

Aaron's answer should work just fine. If it doesn't then I would guess that setupFilePath is not interpretting -f1"d:\space here\file.ext" as you expect it to.

OPINION ALERT The only thing I would add to his answer is to suggest using double quotes and back ticks in order to allow using a variable within the path for argument -f1:

Start-Process -FilePath $setupFilePath -ArgumentList '-a','-s',"-f1`"$pathToVCCS`"" |
Wait-Process

This way you won't have a hard-coded, absolute path in the middle of a long line.



回答3:

On PowerShell v3, this works:

& $setupFilePath -a -s -f1:"d:\some directory\with blanks in a path\fileVCCS.iss"

Using the PSCX echoargs command shows this:

25> echoargs.exe -a -s -f1"d:\some directory\with blanks in a path\fileVCCS.iss"
Arg 0 is <-a>
Arg 1 is <-s>
Arg 2 is <-f1d:\some directory\with blanks in a path\fileVCCS.iss>

Command line:
"C:\Program Files (x86)\PowerShell Community Extensions\Pscx3\Pscx\Apps\EchoArgs.exe"  -a -s "-f1d:\some directory\with blanks in a path\fileVCCS.iss"

On V2 use - note the addition of a backtick on the last double quote:

PS> echoargs.exe -a -s -f1"d:\some directory\with blanks in a path\fileVCCS.iss`"
Arg 0 is <-a>
Arg 1 is <-s>
Arg 2 is <-f1d:\some directory\with blanks in a path\fileVCCS.iss>

Command line:
"C:\Program Files (x86)\PowerShell Community Extensions\Pscx3\Pscx\Apps\EchoArgs.exe"  -a -s -f1"d:\some directory\with blanks in a path\fileVCCS.iss"