1

I am trying to reboot Linux machine from PowerShell script running on Windows 10. I use Posh-SSH module. All other commands I send via SSH session work fine, but reboot has no effect no matter what I tried. Via regular SSH client session (Putty or BitWise) these commands work and device reboots immediately. Below is a sample command:

Invoke-SSHCommand -SSHSession $session -Command "nohup reboot >/dev/null &" -EnsureConnection

I tried with/without nohup. Tried shutdown -r, reboot, reboot -f. Tried using exit command Tried sleep/timeout.

All these worked as expected in SSH console, but nothing worked via Posh-SSH. What am I missing that's different in Posh-SSH?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Alex Michel
  • 416
  • 3
  • 13
  • PowerShell 6 has Restart-Computer. Have you installed PowerShell 6 on the Linux machine? https://github.com/PowerShell/PowerShell – lit Mar 14 '18 at 22:14
  • I can't - it is embedded Linux distro, barely have required commands there – Alex Michel Mar 15 '18 at 10:25
  • Perhaps there will come a time when pwsh is included in Linux distros just as they do bash, ksh, tcsh, etc... – lit Mar 15 '18 at 11:53

1 Answers1

1

Finally I managed to get it working by using stream. Below is code snippet. Start-Sleep is needed before you close the stream, otherwise there's not enough interval for it to run.

$stream=$session.Session.CreateShellStream("ps", 0, 0, 0, 0, 1000)
$stream.WriteLine("reboot")
Start-Sleep 1000
$stream.Close()
Alex Michel
  • 416
  • 3
  • 13