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

vkl*_*vet 13 powershell command-line command-line-arguments

我需要从一个PowerShell脚本启动一个进程并传递这样的参数:-a -s f1d:\ some directory\with path\file.iss中的空白来做到这一点,我写了下面的代码:

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

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

Aar*_*sen 10

我想你可以用Start-Process:

Start-Process -FilePath $setupFilePath -ArgumentList '-a','-s','-f1"d:\some directory\with blanks in a path\fileVCCS.iss"' |
    Wait-Process
Run Code Online (Sandbox Code Playgroud)


Ben*_*bot 5

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

我假设在Windows批处理文件中的等价物将是这样的:

"%setupFilePath%" -a -s -f1"d:\some directory\with blanks in a path\fileVCCS.iss"
Run Code Online (Sandbox Code Playgroud)

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

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

要在你的问题中使用代码片段完成此操作,我会使用返回标记(在1的左侧和转义键下方,不要与单引号混淆;也称为Grave-accent)以逃避内部双引号,如下所示:

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

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

亚伦的答案应该可以正常运作.如果没有,那么我猜想setupFilePath没有-f1"d:\space here\file.ext"像你期望的那样解释.

意见警告我要回答的唯一一点是建议使用双引号和反向标记,以便允许在路径中使用变量-f1:

Start-Process -FilePath $setupFilePath -ArgumentList '-a','-s',"-f1`"$pathToVCCS`"" |
Wait-Process
Run Code Online (Sandbox Code Playgroud)

这样,您就不会在长行中间使用硬编码的绝对路径.


Kei*_*ill 2

在 PowerShell v3 上,这有效:

& $setupFilePath -a -s -f1:"d:\some directory\with blanks in a path\fileVCCS.iss"
Run Code Online (Sandbox Code Playgroud)

使用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"
Run Code Online (Sandbox Code Playgroud)

在 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"
Run Code Online (Sandbox Code Playgroud)