【问题标题】:How can I execute an external program with parameters in PowerShell?如何在 PowerShell 中执行带有参数的外部程序?
【发布时间】:2012-09-10 19:22:16
【问题描述】:

我已经阅读了这个答案stackoverflow 答案,它让我走到了一半。这是我需要做的。

执行这个命令:

"c:\myexe.exe <c:\Users\Me\myanswerfile.txt"

如果我直接从我的 powershell 脚本中运行它

&'c:\myexe.exe <c:\Users\Me\myanswerfile.txt'

我收到此错误:

The term 'C:\myexe.exe <c:\Users\Me\myanswerfile.txt' is not recognized as the name of
a cmdlet, function, script file, or operable program. Check the spelling of the name,or 
if a path was included, verif that the path is correct and try again.

现在我已经尝试了几种变体,包括将原始命令放在一个名为 $cmd 的变量中,然后传递

如果我将“

我被难住了。有什么建议吗?

【问题讨论】:

    标签: powershell


    【解决方案1】:

    如果你想运行一个程序,只需输入它的名字和参数:

    notepad.exe C:\devmy\hi.txt
    

    如果您想运行一个 exe 并将标准输入重定向到它,您的示例似乎是尝试的,请使用:

    Get-Content c:devmy\hi.txt | yourexe.exe 
    

    如果您需要指定程序的完整路径,那么您需要使用 & 和引号,否则 powershell 会认为您定义的是纯字符串:

    &"C:\Program Files (x86)\Notepad++\notepad++.exe"
    

    【讨论】:

      【解决方案2】:

      只需使用&amp; 运算符

      & "Program\path\program.exe" "arg1" "arg2" ....
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-05-24
        • 1970-01-01
        • 1970-01-01
        • 2010-10-03
        • 1970-01-01
        • 1970-01-01
        • 2020-03-21
        相关资源
        最近更新 更多