远程重新启动计算机两次



我有一个场景,需要远程重新启动计算机两次。我的命令:

Invoke-Command -ComputerName $computerName -Credential $cred -ScriptBlock {
workflow Reboot {
Restart-Computer -Wait
Restart-Computer -Wait
}
Reboot
}

但这会返回错误

Failed to restart the computer com1 with the following error message: A system shutdown is in progress.
+ CategoryInfo          : OperationStopped: (com1:String) [Restart-Computer], InvalidOperationException
+ FullyQualifiedErrorId : RestartcomputerFailed,Microsoft.PowerShell.Commands.RestartComputerCommand
+ PSComputerName        : com1

如果您正在重新启动本地计算机(您正在使用远程会话(,则不能使用-Wait

重新启动计算机状态的文档:

重新启动本地计算机时,等待参数无效如果ComputerName参数的值包含远程计算机和本地计算机的名称,则"重新启动计算机"会为"在本地计算机上等待"生成非终止错误,但会等待远程计算机重新启动。

您需要更改命令,使其不使用InvokeCommand:

Restart-Computer -ComputerName $computerName -Credential $cred -Wait

最新更新