【问题标题】:How to mix parameter strings in powershell如何在powershell中混合参数字符串
【发布时间】:2021-05-12 18:27:03
【问题描述】:

我需要在 powershell 中将一些参数传递给这个 start-process 命令。到目前为止,还无法使其将参数传递给应用程序。好像我不能用引号将我的参数括在应用程序中,没有它们,命令可以正常工作。

Start-Process "c:\app\myApp.exe /S '/V /qn AllUsers=1 SN=123-456' " -NoNewWindow -Wait -PassThru

有没有办法在 powershell 的引号内组合引号? ...谢谢

【问题讨论】:

    标签: powershell quotes double-quotes


    【解决方案1】:

    你可以试试这个方法

    Start-Process -FilePath "c:\app\myApp.exe" -ArgumentList "/S","'/V /qn AllUsers=1 SN=123-456'" -NoNewWindow -Wait -PassThru
    

    使用-ArgumentList "arg1","arg2",...

    例如:

    Start-Process -FilePath "python" -ArgumentList "-c","print('hello')" -NoNewWindow -Wait -PassThru
    

    输出:

    Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
    -------  ------    -----      -----     ------     --  -- -----------
         38       3      508       1476       0.02   4932   4 python
    
    hello
    

    【讨论】:

      【解决方案2】:

      您可以将整个参数列表作为单个字符串提供:

      Start-Process -FilePath "c:\app\myApp.exe  -ArgumentList "/S '/V /qn AllUsers=1 SN=123-456'" -NoNewWindow -Wait -PassThru
      

      或者 你可以拆分它们

      Start-Process -FilePath "c:\app\myApp.exe  -ArgumentList "/S","'/V /qn AllUsers=1 SN=123-456'" -NoNewWindow -Wait -PassThru
      

      More information on start-process

      【讨论】:

      • 谢谢 .... 出于某种原因,这有效:-ArgumentList "/S","'/V /qn'" 但这不起作用:-ArgumentList "/S","'/ V /qn","AllUsers=1" 它只是不接受最后一个参数 "foo=123"
      【解决方案3】:

      看起来管道参数是要走的路:-PassThru |等待进程; 这让我们解决了问题。 ... 泰。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-31
        • 1970-01-01
        • 2021-12-19
        • 1970-01-01
        相关资源
        最近更新 更多