Restart Computers in Sequential Order

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

 RESTART_SEQUENTIAL_ORDER.PS1

 

11 thoughts on “Restart Computers in Sequential Order

  1. 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.

    • Just found a formatting error on the code I pasted on this post. It has now been corrected

  2. 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

  3. 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

  4. 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.

    • 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.

Leave a Reply

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