fal*_*lic 14 parameters powershell
我试图从powershell脚本运行外部exe.
这个exe需要4个参数.
我一直在尝试invoke-item,invoke-command和'C:\ program files\mycmd.exe myparam'的每一个组合,在C:\中创建一个快捷方式来摆脱路径中的空格.
我可以使用一个参数,但不能更多.我收到各种错误.
总结一下,如何向exe发送4个参数?
Pet*_*ale 22
如果用手写的话最好.一旦你看到发生了什么,你可以通过在每个参数之间使用逗号来缩短它.
$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
Run Code Online (Sandbox Code Playgroud)
......速记:
& "C:\Program Files\someapp\somecmd.exe" "filename1", "-someswitch", "C:\documents and settings\user\desktop\some other file.txt", "-yetanotherswitch"
Run Code Online (Sandbox Code Playgroud)
Kei*_*ill 12
在简单的情况下,将参数传递给本机exe就像使用内置命令一样简单:
PS> ipconfig /allcompartments /all
Run Code Online (Sandbox Code Playgroud)
指定EXE的完整路径并且该路径包含空格时,可能会遇到问题.例如,如果PowerShell看到这个:
PS> C:\Program Files\Microsoft SDKs\Windows\v7.0\Bin\sn.exe -k .\pubpriv.snk
Run Code Online (Sandbox Code Playgroud)
它将命令解释为"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
Run Code Online (Sandbox Code Playgroud)
我们遇到问题的下一个方面是参数是复杂的和/或使用PowerShell专门解释的字符,例如:
PS> sqlcmd -v user="John Doe" -Q "select '$(user)' as UserName"
Run Code Online (Sandbox Code Playgroud)
这不工作,我们可以通过从工具调试这个PowerShell的社区扩展名为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>
Run Code Online (Sandbox Code Playgroud)
请注意,$(user)
PowerShell会解释和评估Arg3,并生成空字符串.您可以使用单引号而不是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>
Run Code Online (Sandbox Code Playgroud)
如果所有其他方法都失败了,请使用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>
Run Code Online (Sandbox Code Playgroud)
请注意,如果您使用的是PSCX 1.2,则需要使用前缀Start-Process作为前缀 - Microsoft.PowerShell.Management\Start-Process
使用PowerShell的内置Start-Process cmdlet.
归档时间: |
|
查看次数: |
31960 次 |
最近记录: |