Restarting servers is a necessary evil in a Windows administrator’s world. Unfortunately, you cannot not always just restart servers during maintenance as they may have a service dependent on another server. Due to this, you may need to restart your servers in sequential order. Luckily, powershell 3.0 makes this quite easy using the restart-computer commandlet. Please note, the restart-computer commandlet added several important parameters vs the 2.0 version, which includes the following (Good Explanation Here):
- -Delay
- -For
- -Timeout
- -Wait
The above parameters allow you to ensure a server rebooted before moving on in the script. For a full description, see Restart-Computer on TechNet
For our script, we will use a CSV file that lists the server names and the sequence to group them for restarting (Yes we can have multiple servers in the same sequence number). Our powershell script will then read in the the servers.csv, group the servers by their sequence number, and reboot the servers according to the sequence number group. The below powershell script will wait until the servers have fully rebooted before moving to the next sequence number to reboot. You may wish to modify the restart-computer parameters to suit your needs. Additionally, you will need to modify the $filePath and $creds variables accordingly.
SERVERS.CSV
SERVER,SEQUENCE ad1,1 ad2,2 web,2 sql,2 printers,3 files,3 app,4
RESTART_SEQUENTIAL_ORDER.PS1
#========================================================================= # Restart_Sequential_Order.ps1 # VERSION: 1.0 # AUTHOR: Brian Steinmeyer # EMAIL: [email protected] # WEB: https://sigkillit.com # DATE: 12/24/2014 # REQUIREMENTS: # 1) Powershell 3.0 for the following parameters: # -Delay # -For # -Timeout # -Wait # COMMENTS: This script restarts servers in sequential order and does not # not start the next sequence until the previous server(s) rebooted. #========================================================================= # ------ SCRIPT CONFIGURATION ------ $filePath = "servers.csv" $creds = get-credential domain\user # ------ END CONFIGURATION ------ $data = Import-Csv $filePath $objdata = $data | Select SERVER,SEQUENCE | Group SEQUENCE | Select @{n="SEQUENCE";e={$_.Name}},@{n="SERVER";e={$_.Group | ForEach{$_.SERVER}}} | Sort SEQUENCE ForEach ($d in $objdata) { $sequence = $d.SEQUENCE.ToString() $server = $d.SERVER.ToString() write-host "SEQUENCE: $sequence" write-host "------------" write-host "Rebooting: $server" restart-computer -ComputerName "$server" -Force -Wait -Credential $creds }
15 comments
Skip to comment form
restart-computer : Computer name server01
cannot be resolved with the exception: No such host is known.
For anonymity the computer’s name isn’t actually server-01. I have the actual name in the csv file and it reads it but I keep getting this error.
Author
Just found a formatting error on the code I pasted on this post. It has now been corrected
tried your script but somehow cant get it to work. it erros with thw following error:
restart-computer : Computer name svr01
cannot be resolved with the exception: No such host is known.
At line:14 char:2
+ restart-computer -ComputerName “$server” -Force -Wait -WhatIf
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (srv01
:String) [Restart-Computer], InvalidOperationException
+ FullyQualifiedErrorId : AddressResolutionException,Microsoft.PowerShell.Commands.RestartComputerCommand
nvm i already found the solution
Author
Just found a formatting error on the code I pasted on this post. It has now been corrected
Works great if the servers all have different sequence numbers, but I receive the following error if any two have the same sequence number:
restart-computer : Computer name System.Object[] cannot be resolved with the exception: One or more errors occurred..
At C:\Users\user\AppData\Local\Temp\e8266b71-07e1-498c-9a2a-b827152c720c.ps1:30 char:2
+ restart-computer -ComputerName “$server” -Force -Wait -Credential …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (System.Object[]:String) [Restart-Computer], InvalidOperationException
+ FullyQualifiedErrorId : AddressResolutionException,Microsoft.PowerShell.Commands.RestartComputerCommand
I am getting the same thing. If there are repeats for the sequence number it doesn’t work.
I had something odd happen with this, It only worked on some of the servers I was trying to reboot. One group worked perfectly, while others were hit and miss. Here is the error I get:
SEQUENCE: 1
————
Rebooting: System.Object[]
ERROR: restart-computer : Computer name System.Object[] cannot be resolved with the exception: One or more errors occurred..
RestartDC.ps1 (32, 2): ERROR: At Line: 32 char: 2
ERROR: + restart-computer -ComputerName $server -Force -Wait -Credential $ …
ERROR: + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR: + CategoryInfo : InvalidArgument: (System.Object[]:String) [Restart-Computer], InvalidOperationException
ERROR: + FullyQualifiedErrorId : AddressResolutionException,Microsoft.PowerShell.Commands.RestartComputerCommand
ERROR:
Any thoughts?
And by the way, the one group that worked was amazing. Perfectly rebooted 4 servers in group. All I had to do was look at it every once in a while to make sure it was still going.
I also checked to make sure the names were correct, I could ping, etc.
Author
There’s no error checking on what it’s reading in from the CSV file. There’s most likely something incorrectly formatted in there. Make sure there’s no extra white space, blank lines, etc. You may want to open it with something like notepad++ which allows you to see any extra stuff pretty easy
Checked with Notepad++ and there are no additional spaces. I read some of the other comments and determined I was having the same problem as the person above – if there are repeats in the sequence number it doesn’t work.
Changes for repeats in the sequence (bad english, sorry):
$sequence = $d.SEQUENCE.ToString()
$ofs = ‘,’
$server = $d.SERVER
and
Restart-Computer -ComputerName $server -Force -Wait -Credential $creds
# —— SCRIPT CONFIGURATION ——
$Servers=$(Read-Host -prompt “Enter servers list”)
$ScriptDir = Split-Path $script:MyInvocation.MyCommand.Path
$filePath = $ScriptDir + “\” + $Servers +”.csv”
$creds = get-credential
# —— END CONFIGURATION ——
$data = Import-Csv $filePath
$objdata = $data | Select SERVER,SEQUENCE | Group SEQUENCE | Select @{n=”SEQUENCE”;e={$_.Name}},@{n=”SERVER”;e={$_.Group | ForEach{$_.SERVER}}} | Sort SEQUENCE
ForEach ($d in $objdata) {
$sequence = $d.SEQUENCE.ToString()
$ofs = ‘,’
$server = $d.SERVER
write-host ” ”
write-host “SEQUENCE: $sequence”
write-host “————”
write-host “Rebooting: $server”
Restart-Computer -ComputerName $server -Force -Wait -Credential $creds
}
Multiple server must restart in sequential order and should stop if any of the server fails. Also need to check services status once the servers are up
Can you please suggest?
Multiple server must restart in sequential order and should stop if any of the server fails. Also need to check services status once the servers are up
Can you please suggest?