Reboot server remotely via PowerShell

Reboot 🙂 I guess that all of the server administrators had this situation when server was not responding. If you don’t have access to iDRAC, iLO .etc the only way to fix the issue will be rebooting server remotely. So today I would like to show you couple of ways to do that.

The easiest way is to run cmd as an administrator and use command:
SHUTDOWN /r /f /t 0 /m \\servername /c “Emergency reboot”

For more information about this command please refer to:
https://technet.microsoft.com/pl-pl/library/bb491003.aspx

Other way to reboot server is to use PowerShell command restart-computer with force parameter and admin account credentials:

Restart-Computer -ComputerName $Server -Force -Credential pawel.janowicz 

# Or multiple servers from txt file
Restart-Computer (Get-Content "c:\temp\servers.txt")

Unfortunately, sometimes it doesn’t work properly and we might get following error message:
“The RPC server is unavailable”

New-PSSession option comes to our rescue 🙂

To create new session with affected machine $server we need to provide our admin account credentials $cred. Another line stands for running the same restart command with force parameter but in this case we will pass it into ScriptBlock. Last step is to remove existing session to avoid making any mistakes.

Final script:

    
$server ='DCO1'

#Get credentials
$cred = $Host.ui.PromptForCredential("PowerShellBros","Please use admin account to login (ie. pawel.janowicz)","$ENV:Username","")
 
$session = new-pssession -computer $server -credential $cred
invoke-command  -Session $session -ScriptBlock { restart-computer -Force }
Remove-PSSession $session 
    

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.