Powershell:'&'的详细等价物呼叫运营商?

时间:2012-02-27 18:10:16

标签: powershell operators

'&'是否有详细的完全等价物调用运算符与scriptblocks一起使用?

喜欢这个伪代码:

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; } %*')

我知道我仍然需要为长期争论而逃避引用。

感谢。

4 个答案:

答案 0 :(得分:2)

与我所知道的“详细”等同物最接近的是:

{ "Arguments are:"; $args; }.InvokeReturnAsIs("One","Two","Three word argument")

答案 1 :(得分:2)

您可以定义一个函数,使您提到的伪代码起作用:

function call ($script){
    & $script $args
}

call { "Arguments are:"; $args; } One Two "Three word argument"

答案 2 :(得分:2)

您可以将批处理文件参数传递给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"

答案 3 :(得分:1)

你不需要逃脱&符号。这应该可以正常工作:

@ECHO OFF
START powershell.exe -noexit -Command "& { \"Batch file arguments are:\"; $args; } %*"

如果你这样称呼它:

mybatch.cmd One Two 'Three word argument'
相关问题