将带有管道的参数传递到powershell脚本中-如何转义管道?
在powershell中逃离chars真是愚蠢。我已经尝试了所有我能找到的方法,试图在传入powershell脚本的参数中转义这个管道,但我绝对无法转义它。这是胡说八道。我确保以单引号传递参数,并尝试了所有powershell/windows转义字符(`、`、\、\、\、^、^^)。非常感谢您的帮助 测试脚本示例:将带有管道的参数传递到powershell脚本中-如何转义管道?,powershell,Powershell,在powershell中逃离chars真是愚蠢。我已经尝试了所有我能找到的方法,试图在传入powershell脚本的参数中转义这个管道,但我绝对无法转义它。这是胡说八道。我确保以单引号传递参数,并尝试了所有powershell/windows转义字符(`、`、\、\、\、^、^^)。非常感谢您的帮助 测试脚本示例: $cmd = $Args[0] #set first parameter received to $cmd to run with Invoke-Expression (must w
$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.
请注意:
cmd
。直接给PowerShell打电话-InputFormat
可以是文本或XML<代码>无在此处不是有效值从cmd开始,管道的转义字符将是克拉^这是有效的。我现在能逃脱我扔给它的每一个特殊的字符。在我的例子中,-InputFormat none实际上很重要。如果未包含此内容,我的powershell会话将挂起并等待更多输入。我读到过一篇文章,-InputFormat none在我使用它时是有效的(cmd也是必要的,因为我通过php页面运行这些命令)。再次感谢你,这太棒了。
powershell "&{ &'D:\test_powershell\app\test_wrapper.ps1' 'test 1 | test2' }"