使用任意脚本块调用Powershell命令
我有一个在远程Windows服务器上成功执行的命令使用任意脚本块调用Powershell命令,powershell,window,winrm,Powershell,Window,Winrm,我有一个在远程Windows服务器上成功执行的命令 Invoke-Command -ComputerName $ComputerName -UseSSL -SessionOption $sessionOptions -Credential $cred -ScriptBlock {Start-Service "Test Service"} 我试图将脚本块作为变量传递,以便在特定服务器上执行任意命令,但似乎无法成功传递ScriptBlock参数。代码执行时没有任何错误,但不会启动远程服务 $Sc
Invoke-Command -ComputerName $ComputerName -UseSSL -SessionOption $sessionOptions -Credential $cred -ScriptBlock
{Start-Service "Test Service"}
我试图将脚本块作为变量传递,以便在特定服务器上执行任意命令,但似乎无法成功传递ScriptBlock参数。代码执行时没有任何错误,但不会启动远程服务
$ScriptBlock = Start-Service "Test Service"
Invoke-Command -ComputerName $ComputerName -UseSSL -SessionOption $sessionOptions -Credential $cred -ScriptBlock
{$args[0]} -ArgumentList $ScriptBlock
将代码括在
{}
中,以便在将其分配给$scriptblock
变量时定义scriptblock文本,然后将其作为参数传递给-scriptblock
参数:
$ScriptBlock = { Start-Service "Test Service" }
Invoke-Command -ScriptBlock $ScriptBlock -ComputerName $ComputerName -UseSSL -SessionOption $sessionOptions -Credential $cred
就这样。真不敢相信我竟然忽略了,谢谢!