两次远程重新启动计算机

时间:2018-11-20 09:30:07

标签: powershell remote-access invoke-command

我有一种情况,我需要远程重新启动计算机两次。 我的命令:

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

1 个答案:

答案 0 :(得分:2)

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

Restart-Computer状态的文档:

  

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

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

Restart-Computer -ComputerName $computerName -Credential $cred -Wait
相关问题