Reboot Dependent Servers in sequence Powershell


This topic contains 2 replies, has 3 voices, and was last updated by  Dave Wyatt 2 years, 9 months ago.

  • Author
  • #21410

    Karunanidhi Mishra

    Hello Everyone,

    I am horribly stuck into a challenge for designing a powershell code to reboot a list of servers from a CSV depending on a Sequence Number.










    This is how the CSV looks like with Server Name and its Sequence to be rebooted.


    * Servers with same sequence number should be rebooted at a time together followed by a delay and then the Next sequence server to be rebooted.

    Ex, ServerA & ServerG are with same sequence number '1' hence to be rebooted together and then ServerD & ServerE since its sequence number is 2..followed by 3 and 4 and 5..

    I am seeking for suggestions on how shall I proceed or any bits or piece of code that would help me..

    Thanks in Advance _/\_

  • #21412

    Sikander Mirza

    Restart-Computer is the Cmdlet which will restart the computers, the paramater -ComputerName
    accepts multiple computer names at a time.
    you can use this as simple as you can like.
    1. You can run it through the console as
    Restart-Computer -Computername, [or]
    Restart-Computer -Computername ServerA, ServerB.
    you can save this as a script also such as with name RestartSeq1.ps1

    2. you can also pick server names from a csv. [have seperate sequence server names in seperate CSV's]
    Import-CSV -Path D:\Names.csv | Restart-Computer

    3. if you have Server names in a text file with one name per line.
    Get-Content -Path D:\Names.txt | Restart-Computer

    you can learn more on this by typing in the powershell console.

    PS:\>help Restart-Computer -Full
    PS:\>Get-Command Restart-Computer -Syntax

  • #21415

    Dave Wyatt

    Looks like you want a combination of Import-Csv, Group-Object, Sort-Object, and Restart-Computer. Something along these lines:

    $data = Import-Csv DependentList.csv
    $sequenceGroups = $data | Group-Object -Property Sequence
    $sorted = $sequenceGroups | Sort-Object -Property { [int] $_.Name }
    foreach ($sequenceGroup in $sorted)
        $computerNames = $sequenceGroup.Group | Select-Object -ExpandProperty CI
        Restart-Computer -ComputerName $computerNames -Wait

    The generic *-Object cmdlets are, IMO, one of the main reasons to start using PowerShell in the first place. With the object-based pipeline and this built-in query language that you can apply to anything, you can do some really impressive things with very few lines of code. I think Group-Object and Sort-Object get overlooked quite a bit.

You must be logged in to reply to this topic.