简体   繁体   English

powershell将多个参数发送到外部命令

[英]powershell sending multiple parameter to a external command

I am trying to run a external exe from a powershell script. 我试图从powershell脚本运行外部exe。

This exe wants 4 parameters. 这个exe需要4个参数。

I have been trying every combo of invoke-item, invoke-command, & 'C:\\program files\\mycmd.exe myparam', made a shortcut in C:\\ to get rid of the spaces in the path. 我一直在尝试invoke-item,invoke-command和'C:\\ program files \\ mycmd.exe myparam'的每一个组合,在C:\\中创建一个快捷方式来摆脱路径中的空格。

I can make it work with one parameter, but not with more. 我可以使用一个参数,但不能更多。 I get various errors. 我收到各种错误。

To sum up, how do you send 4 parameters to an exe? 总结一下,如何向exe发送4个参数?

It's best if shown in longhand. 如果用手写的话最好。 Once you see what's going on, you can shorten it down by just using commas between each argument. 一旦你看到发生了什么,你可以通过在每个参数之间使用逗号来缩短它。

$arg1 = "filename1"
$arg2 = "-someswitch"
$arg3 = "C:\documents and settings\user\desktop\some other file.txt"
$arg4 = "-yetanotherswitch"

$allArgs = @($arg1, $arg2, $arg3, $arg4)

& "C:\Program Files\someapp\somecmd.exe" $allArgs

... shorthand: ......速记:

& "C:\Program Files\someapp\somecmd.exe" "filename1", "-someswitch", "C:\documents and settings\user\desktop\some other file.txt", "-yetanotherswitch"

In the easy case, passing arguments to a native exe is as simple as using a built-in command: 在简单的情况下,将参数传递给本机exe就像使用内置命令一样简单:

PS> ipconfig /allcompartments /all

You can run into problems when you specify a full path to an EXE and that path contains spaces. 指定EXE的完整路径并且该路径包含空格时,可能会遇到问题。 For example if PowerShell sees this: 例如,如果PowerShell看到这个:

PS> C:\Program Files\Microsoft SDKs\Windows\v7.0\Bin\sn.exe -k .\pubpriv.snk

It interprets the command to be "C:\\Program" and "Files\\Microsoft" as the first parameter, "SDKs\\Windows\\v7.0\\Bin\\sn.exe" as the second parameter, etc. The simple solution is to put the path in a string use the invocation operator & to invoke the command named by the path eg: 它将命令解释为“C:\\ Program”和“Files \\ Microsoft”作为第一个参数,“SDKs \\ Windows \\ v7.0 \\ Bin \\ sn.exe”作为第二个参数等。简单的解决方案是将路径放在字符串中使用调用操作符&调用路径命名的命令,例如:

PS> & 'C:\Program Files\Microsoft SDKs\Windows\v7.0\Bin\sn.exe' -k .\pubpriv.snk

The next area we run into problems with is when the arguments are complex and/or use characters that PowerShell interprets specially eg: 我们遇到问题的下一个方面是参数是复杂的和/或使用PowerShell专门解释的字符,例如:

PS> sqlcmd -v user="John Doe" -Q "select '$(user)' as UserName"

This doesn't work and we can debug this by using a tool from the PowerShell Community Extensions called echoargs.exe which shows you exactly how the native EXE receives the arguments from PowerShell. 这不起作用,我们可以使用名为echoargs.exePowerShell社区扩展 echoargs.exe的工具来调试它,它显示了本机EXE如何从PowerShell接收参数。

PS> echoargs -v user="John Doe" -Q "select '$(user)' as UserName"
The term 'user' is not recognized as the name of a cmdlet, function, 
script file, or operable program. Check the spelling of the name, ...
<snip>

Arg 0 is <-v>
Arg 1 is <user=John Doe>
Arg 2 is <-Q>
Arg 3 is <select '' as UserName>

Note that with Arg3 $(user) is interpreted & evaluated by PowerShell and results in an empty string. 请注意,使用PowerShell解释和评估Arg3 $(user)并生成空字符串。 You can fix this problem and a good number of similar issues by using single quotes instead of double qoutes unless you really need PowerShell to evaluate a variable eg: 您可以使用单引号而不是double qoutes来解决此问题和大量类似问题,除非您确实需要PowerShell来评估变量,例如:

PS> echoargs -v user="John Doe" -Q 'select "$(user)" as UserName'
Arg 0 is <-v>
Arg 1 is <user=John Doe>
Arg 2 is <-Q>
Arg 3 is <select $(user) as UserName>

If all else fails, use a here string and Start-Process like so: 如果所有其他方法都失败了,请使用here字符串和Start-Process,如下所示:

PS> Start-Process echoargs -Arg @'
>> -v user="John Doe" -Q "select '$(user)' as UserName"
>> '@ -Wait -NoNewWindow
>>
Arg 0 is <-v>
Arg 1 is <user=John Doe>
Arg 2 is <-Q>
Arg 3 is <select '$(user)' as UserName>

Note if you are using PSCX 1.2 you will need to prefix Start-Process like so - Microsoft.PowerShell.Management\\Start-Process to use PowerShell's built-in Start-Process cmdlet. 请注意,如果您使用的是PSCX 1.2,则需要使用前缀Start-Process作为前缀 - Microsoft.PowerShell.Management\\Start-Process以使用PowerShell的内置Start-Process cmdlet。

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

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