使用包含空白路径的args启动进程

时间:2013-07-09 14:18:30

标签: powershell command-line command-line-arguments

我需要从powershell脚本开始一个进程并传递这样的参数: -a -s f1d:\某个目录\在路径\ file.iss中有空格 要做到这一点,我写下以下代码:

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

结果进程启动但最后一个参数:-f1d:\ some directory \ with path \ file.iss中的空格 没有正确传递。请帮助

3 个答案:

答案 0 :(得分:9)

我认为你可以使用Start-Process

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

答案 1 :(得分:5)

我理解你的问题是: 如何传递多个参数来启动其中一个参数有空格的进程?

我假设Windows批处理文件中的等效文件类似于:

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

其中双引号允许接收进程(在本例中为 setupFilePath )接收三个参数:

  1. -a
  2. -s
  3. -f1"d:\some directory\with blanks in a path\fileVCCS.iss"
  4. 要使用你问题中的代码片段完成此操作,我会使用返回标记(位于1左侧和退出键下方,不要与单引号混淆;也称为Grave-accent )以这样的方式逃避内部双引号:

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

    请注意,除了使用反向标记之外,我还将参数列表周围的单引号更改为双引号。这是必要的,因为单引号不允许我们在这里需要转义(http://ss64.com/ps/syntax-esc.html)。

    Aaron's answer应该可以正常工作。如果它没有,那么我猜想 setupFilePath 并没有像你期望的那样解释-f1"d:\space here\file.ext"

    OPINION ALERT 我唯一要补充的是建议使用双引号和后退标记以允许在参数-f1的路径中使用变量:

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

    这样你就不会在长行的中间有一条硬编码的绝对路径。

答案 2 :(得分:2)

在PowerShell v3上,这可以:

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

使用PSCX echoargs命令显示:

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"

在使用V2时 - 请注意在最后一个双引号上添加反引号:

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"