0

I want to reboot several computers remotely using PowerShell and wait until the reboot is completed, before continuing the script. I've stumbled across the "restart-computer" cmdlet and its "wait" switch parameter. Sadly, it seems like it's only usable with WinRM as the remoting protocol and not with SSH. We want to only use SSH for remote connections with PowerShell going forward and stop using WinRM altogether.

Is there a possibility I can implement the same functionality but with SSH as the protocol?

Ramhound
  • 44,080

1 Answers1

1

The simplest answer is that powershell can just invoke the built-in ssh.exe like other shells would:

# restart remote server over SSH:
ssh.exe user@hostname shutdown -r -t 0

A longer answer is that powershell 6+ can use the SSH protocol to establish remote powershell sessions, which have some more features than plain SSH. See PowerShell remoting over SSH for details, but the basics are:

  • Powershell 6+ must be installed on both the local and remote systems
  • On the remote server, an SSH subsystem for powershell should be running
  • Commands that use PS Remoting like Invoke-Command can use this directly by specifying -HostName instead of -ComputerName.
  • Requirements are the same for linux and windows clients/servers (in case you're running powershell on linux)

When it's configured properly, you can restart a remote server using SSH like:

Invoke-Command -HostName MyServer -UserName MyUser -ScriptBlock {Restart-Computer}

and manually check for the remote server to come back up like:

Sleep 30  ## wait before checking
$SSH=$null
While(-not $SSH){
  Try  {$SSH = New-PSSession -HostName 'MyServer' -ea Stop}
  Catch{Write-Host 'Waiting for reboot...'}
  sleep 10
}
Cpt.Whale
  • 10,914