function test-scriptblock {
1..10 }
function caller ([scriptblock]$runthis) {
& $runthis
}

以下工作正常。
caller -runthis ${function:test-scriptblock}

这行不通
invoke-command -ComputerName localhost -ScriptBlock ${function:caller} -ArgumentList ${function:test-scriptblock}

Cannot process argument transformation on parameter 'runthis'. Cannot convert the "
1..10 " value of type "System.String" to type "System.Management.Automation.ScriptBlock".
+ CategoryInfo          : InvalidData: (:) [], ParameterBindin...mationException
+ FullyQualifiedErrorId : ParameterArgumentTransformationError

最佳答案

我确认这是一个“已知问题”。虽然在大多数情况下,在远程处理脚本块时可以像脚本块一样进行反流,但是使用ArgumentList却不行,所以我改为

function Caller($runthis)
{
   $runthis = [Scriptblock]::Create($runthis)
   &$runthis
}

关于powershell - 为什么在invoke-commands参数列表中作为参数传递的脚本块失败?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7180838/

10-11 09:06