【问题标题】:powershell sending multiple parameter to a external commandpowershell 向外部命令发送多个参数
【发布时间】:2011-01-09 08:59:37
【问题描述】:

我正在尝试从 powershell 脚本运行外部 exe。

这个 exe 需要 4 个参数。

我一直在尝试调用项目、调用命令和“C:\program files\mycmd.exe myparam”的每一个组合,在 C:\ 中创建了一个快捷方式来消除路径中的空格。

我可以使用一个参数,但不能使用更多参数。我收到各种错误。

总结一下,怎么给一个exe发送4个参数?

【问题讨论】:

    标签: powershell parameters


    【解决方案1】:

    最好以速记形式显示。一旦你看到发生了什么,你可以通过在每个参数之间使用逗号来缩短它。

    $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
    

    ...简写:

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

    【讨论】:

    • 注意 & 'C:\program files\someapp\somecmd.exe" -arg $allArgs - -arg 是不必要的,除非您打算将 -arg 本身作为参数传递给 somecmd.exe。
    • 是的,无论出于何种原因,我今天提到了这个,并注意到带有 -arg 的代码行甚至不起作用!糟糕/已删除。
    • 我认为最后一行缺少 ( ... ):& "somecmd.exe" ("filename1", "-some...")
    【解决方案2】:

    在简单的情况下,将参数传递给本机 exe 就像使用内置命令一样简单:

    PS> ipconfig /allcompartments /all
    

    当您指定 EXE 的完整路径并且该路径包含空格时,您可能会遇到问题。例如,如果 PowerShell 看到这个:

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

    它将命令解释为“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
    

    我们遇到的下一个问题是当参数很复杂和/或使用 PowerShell 专门解释的字符时,例如:

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

    这不起作用,我们可以使用 PowerShell Community Extensions 中的一个名为 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>
    

    请注意,使用 Arg3 $(user) 由 PowerShell 解释和评估,并产生一个空字符串。您可以通过使用单引号而不是双引号来解决此问题和大量类似问题,除非您确实需要 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>
    

    如果所有其他方法都失败,请使用此处的字符串和 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>
    

    请注意,如果您使用的是 PSCX 1.2,则需要像这样为 Start-Process 添加前缀 - Microsoft.PowerShell.Management\Start-Process 以使用 PowerShell 的内置 Start-Process cmdlet。

    【讨论】:

    • 嗯;看到这让我再次想知道:是否有充分的理由保留 PowerShell v2 包含的 cmdlet 的 PSCX 版本?由于其完全不同的语义,Get-Random 让我几次犯错。
    • 不,没有。因此,我们已将具有名称冲突的三个 cmdlet 移动到 PSCX 2.0 (Pscx.Deprecated.psd1) 中的单独模块中。有一个 2.0 测试版可供下载,但由于它是基于模块的,它仅适用于 PowerShell 2.0。
    • 我刚刚注意到这一点:PowerShell and external commands done right。几乎相同的建议。
    • @ChristopherGalpin 链接已损坏,现在位于web.archive.org/web/20180317092333/http://edgylogic.com/blog/…
    猜你喜欢
    • 1970-01-01
    • 2017-11-20
    • 2012-03-15
    • 1970-01-01
    • 2012-12-10
    • 2011-10-05
    • 1970-01-01
    • 2019-02-16
    • 1970-01-01
    相关资源
    最近更新 更多