对于与脚本 block 一起使用的“&”调用运算符,是否有一个冗长的确切等价物?
像这样的伪代码:
CALL { "Arguments are:"; $args; } One Two "Three word argument"
替换:
& { "Arguments are:"; $args; } One Two "Three word argument"
编辑:
我需要它的原因是将批处理文件参数传递给 powershell。
这失败了:
@ECHO OFF
START powershell.exe -noexit -Command "^& { \"Batch file arguments are:\"; $args; } %*"
我可以这样做,但第一种方法更干净:
@ECHO OFF
START powershell.exe -noexit -Command Invoke-Expression $('^& { \"Batch file arguments are:\"; $args; } %*')
我知道我仍然需要对长参数转义引号。
谢谢。
最佳答案
您可以像这样将批处理文件参数传递给 PowerShell:
@ECHO off
SETLOCAL
SET PS_ARGS=%*
IF DEFINED PS_ARGS SET PS_ARGS=%PS_ARGS:"=\"%
START powershell.exe -NoExit -Command "& { \"Batch file arguments are:\"; $args; } %PS_ARGS%"
它会转义引号,所以你可以这样调用它:
MyBatch.bat one two "Three word argument"
关于Powershell: '&' 调用运算符(operator)的详细等效项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9469950/