-ArgumentList

时间:2016-01-31 17:53:54

标签: windows powershell

我想知道是否有更多专业知识的人可以帮助我解决我在-ArgumentList使用Start-Process时使用变量时遇到的一些问题。

如果我在不使用Start-Process

的情况下运行Exe
.\DeploymentServer.UI.CommandLine.exe register --estNumber $Number --postcode $PostCode --password $Password

一切正常,命令运行,软件已注册。

如果我尝试

Start-Process .\DeploymentServer.UI.CommandLine.exe -ArgumentList "register --estNumber $Number --postcode $PostCode --password $Password" -Wait -NoNewWindow

$Arguments = "register --estNumber $Number --postcode $PostCode --password $Password"
Start-Process .\DeploymentServer.UI.CommandLine.exe -ArgumentList $Arguments -NoNewWindow -Wait

命令运行但无法注册,说明它与提供的详细信息不匹配。所以我假设问题在于将参​​数传递给Start-Process,或者-ArgumentList解释字符串中的变量。我错过了一些非常简单的东西吗?可能与$中的-ArgumentList

有关

2 个答案:

答案 0 :(得分:7)

您的$postcode中有空格,因此您需要将参数放在引号中:

Start-Process .\DeploymentServer.UI.CommandLine.exe -ArgumentList "register --estNumber $Number --postcode `"$PostCode`" --password $Password" -Wait -NoNewWindow

答案 1 :(得分:0)

我在网上遇到了几篇文章,说要包装一个Start-Process -ArgumentList参数,该参数包含2层双精度引号中的空格,并用反斜杠`来使内部双引号转义,但是至少在上下文中需要它,那没用。我找到了一个在概念上相似的解决方案,但确实奏效了,即在外部使用一组单引号,在内部使用双引号使用“传统”反斜杠转义序列。在此PowerShell问题文章中,我被引导使用这种方法:

https://github.com/PowerShell/PowerShell/issues/5576

该示例对我有用(从Start-Process运行PS cmd.exe命令):

C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe Start-Process -FilePath 'C:\Program Files (x86)\Example\Test.exe' -ArgumentList 'arg1','\"arg 2 w spaces\"','arg3'