【问题标题】:Passing parameter with a pipe in it into a powershell script - how do I escape the pipe?将带有管道的参数传递到powershell脚本中-如何转义管道?
【发布时间】:2012-07-21 23:42:25
【问题描述】:

在 powershell 中转义字符只是愚蠢的。我已经尝试了所有我能找到的方法来尝试在传递给 powershell 脚本的参数中转义这个管道,但我绝对无法逃脱它。真是疯了。我确保在单引号中传递参数,并尝试了所有的 powershell/windows 转义字符(`,``,\,\,^,^^)。非常感谢任何帮助。

测试脚本示例:

$cmd = $Args[0] #set first parameter received to $cmd to run with Invoke-Expression (must wrap param in single quotes if it contains spaces)  
write-host $cmd

使用包含命令的管道调用测试脚本:

C:\Windows\System32\cmd.exe /c powershell -executionPolicy Unrestricted -InputFormat none "D:\test_powershell\app\test_wrapper.ps1" 'test1 | test2'

结果:

'test2'' is not recognized as an internal or external command,
operable program or batch file.

【问题讨论】:

    标签: powershell


    【解决方案1】:

    一些注意事项:

    1. 你不需要cmd。只需直接调用 PowerShell。
    2. -InputFormat 可以是文本或 XML。 None 在此处不是有效值。
    3. 使用以下内容(如果出现转义问题,帮助中也会提到):

      powershell "&{ &'D:\test_powershell\app\test_wrapper.ps1' 'test 1 | test2' }"
      

    【讨论】:

    • 这行得通。我现在能够逃脱我扔给它的每一个特殊字符。 -InputFormat none 在我的情况下实际上很重要。如果我不包括这个,我的 powershell 会话将挂起并等待更多输入。我读过 -InputFormat none 在以我使用它的方式使用时是有效的(由于我通过 php 页面运行这些命令,因此 cmd 也是必需的)。再次感谢,这太棒了。
    【解决方案2】:

    在 cmd 中,管道的转义字符将是克拉 ^

    【讨论】:

      猜你喜欢
      • 2016-07-03
      • 1970-01-01
      • 2019-05-12
      • 2019-05-14
      • 2019-12-19
      • 2021-01-11
      • 1970-01-01
      • 1970-01-01
      • 2015-03-20
      相关资源
      最近更新 更多