Reputation: 436
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?
Upvotes: 1
Views: 1330
Reputation: 436
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()
Upvotes: 1