远程重启电脑两次

Remotely reboot computer twice

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

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

但是这个returns错误

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

Restart-Computer 的文档说明:

The Wait parameter is not valid when you are restarting the local computer. If the value of the ComputerName parameter contains the names of remote computers and the local computer, Restart-Computer generates a non-terminating error for Wait on the local computer, but it waits for the remote computers to restart.

您需要更改命令,使其不使用 Invoke-Command:

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