如何在“调用表达式”中使用变量作为命令的参数[重复]

问题描述 投票:0回答:1

这个问题在这里已有答案:

我收到了错误

无法将参数绑定到参数'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 }

我如何使用这样的变量?我有这个要求,所以我不能直接使用它,它需要是动态的。

powershell invoke-command
1个回答
1
投票

您可以将-ArgumentList参数与Invoke-Command一起使用,也可以使用$using:访问您的变量,如下所示:

Invoke-Command -ComputerName $systemName -credential $credentials -ScriptBlock {
    Invoke-Expression -Command: $using:command
}
© www.soinside.com 2019 - 2024. All rights reserved.