这个问题在这里已有答案:
我收到了错误
无法将参数绑定到参数'Command',因为它为null。 + CategoryInfo:InvalidData :( :) [Invoke-Expression],ParameterBindingValidationException + FullyQualifiedErrorId:ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.InvokeExpressionCommand + PSComputerName:X
当我尝试使用
$command = "cmd.exe /c $($currentLocation)/Ping.cmd"
Invoke-Command -ComputerName $systemName -credential $credentials -ScriptBlock {Invoke-Expression -Command: $command }
我如何使用这样的变量?我有这个要求,所以我不能直接使用它,它需要是动态的。
您可以将-ArgumentList
参数与Invoke-Command
一起使用,也可以使用$using:
访问您的变量,如下所示:
Invoke-Command -ComputerName $systemName -credential $credentials -ScriptBlock {
Invoke-Expression -Command: $using:command
}