简体   繁体   English

-ArgumentList 中的启动进程变量

[英]Start-Process Variables In -ArgumentList

I was wondering if someone with more expertise could help me with a little problem I'm having with using Variables in -ArgumentList when using Start-Process .我想知道是否有更多专业知识的人可以帮助我解决在使用Start-Process时在-ArgumentList使用变量时遇到的一个小问题。

If I run the Exe without using Start-Process如果我在不使用Start-Process情况下运行 Exe

.\DeploymentServer.UI.CommandLine.exe register --estNumber $Number --postcode $PostCode --password $Password

everything works fine, the command runs and the software is registered.一切正常,命令运行,软件注册。

If I try如果我尝试

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

or或者

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

the command runs but is unable to register, stating that it can not match the details provided.该命令运行但无法注册,说明它无法匹配提供的详细信息。 So I'm assuming the issue lies either in the passing of the arguments to Start-Process , or -ArgumentList interpreting the variables in the string.所以我假设问题在于将参​​数传递给Start-Process ,或者-ArgumentList解释字符串中的变量。 Am I missing something really simple here?我在这里错过了一些非常简单的东西吗? Possibly to do with the $ in the -ArgumentList ?可能与-ArgumentList$有关?

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

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

I encountered several posts online saying to wrap a Start-Process -ArgumentList argument containing spaces in 2 layers of doubles quotes, escaping the inner double quotes with a back tick `, but at least in the context I needed it, that didn't work.我在网上遇到几篇帖子说要在 2 层双引号中包含包含空格的Start-Process -ArgumentList参数,用反勾号转义内部双引号,但至少在我需要它的上下文中,这不起作用. I found a conceptually similar solution which did work, however, ie a set of single quotes on the outside and a "traditional" backslash escape sequence on inner double quotes.我发现了一个概念上类似的解决方案,但它确实有效,即外部的一组单引号和内部双引号上的“传统”反斜杠转义序列。 I was guided to using this approach per this PowerShell issue post:根据这个 PowerShell 问题帖子,我被引导使用这种方法:

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

This example works for me (running a PS Start-Process command from cmd.exe ):这个例子对我有用(从cmd.exe运行 PS Start-Process命令):

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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM