Remotely reboot computer twice

2019-07-22 15:03发布

问题:

I have a scenario where I need to reboot a computer twice, remotely. My command:

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

But this returns the error

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

回答1:

You can't use -Wait if you are restarting the local computer (which you are doing with the remote session).

Documentation for Restart-Computer states:

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.

You will need to change your command so it does not use Invoke-Command:

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