windows -ArgumentList 中的启动进程变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35117204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:27:11  来源:igfitidea点击:

Start-Process Variables In -ArgumentList

windowspowershell

提问by Jamie

I was wondering if someone with more expertise could help me with a little problem I'm having with using Variables in -ArgumentListwhen using Start-Process.

我想知道是否有更多专业知识的人可以帮助我解决在-ArgumentList使用Start-Process.

If I run the Exe without using Start-Process

如果我在不使用的情况下运行 Exe Start-Process

.\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 -ArgumentListinterpreting the variables in the string. Am I missing something really simple here? Possibly to do with the $in the -ArgumentList?

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

回答by Ansgar Wiechers

You have a space in your $postcode, so you need to put the argument in quotes:

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

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

回答by BuvinJ

I encountered several posts online saying to wrap a Start-Process-ArgumentListargument 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. I found a conceptually similar solution which did work, however, i.e. 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:

我在网上看到几篇帖子说将Start-Process-ArgumentList包含空格的参数包装在 2 层双引号中,用反勾号转义内部双引号,但至少在我需要它的上下文中,这是行不通的。然而,我发现了一个概念上类似的解决方案,它确实有效,即外部的一组单引号和内部双引号上的“传统”反斜杠转义序列。根据这个 PowerShell 问题帖子,我被引导使用这种方法:

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

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

This example works for me (running a PS Start-Processcommand from cmd.exe):

这个例子对我有用(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'