Powershell: '&' 调用运算符(operator)的详细等效项?

标签 powershell operators

对于与脚本 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/

相关文章:

windows - 当目标路径包含表情符号字符时,复制快捷方式文件(*.lnk)的目标

powershell - 当脚本设置为 “Run As Admin”时,有没有办法在Powershell中映射本地驱动器?

java - Java中 "instanceof"的使用

bash - 为什么 bash 对文本和数字条件表达式使用不直观的运算符?

ruby - 关于 ruby​​ 中重写 + 运算符的问题

azure - 使用 token 在 Azure 应用程序 URL 上调用 WebRequest

powershell - 将换行符添加到Out-File cmdlet

regex - PowerShell - 用于更改多值字段中分隔符的正则表达式

c++ - 胡萝卜括号在 C++ 中有什么作用?

C++ 运算符重载 - 从类中转换