Reboot Dependent Servers in sequence Powershell

Tagged: 

This topic contains 2 replies, has 3 voices, and was last updated by Profile photo of Dave Wyatt Dave Wyatt 1 year, 11 months ago.

  • Author
    Posts
  • #21410
    Profile photo of Karunanidhi Mishra
    Karunanidhi Mishra
    Participant

    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.

    DependentList.csv

    CI,Sequence

    serverA,1

    serverB,3

    serverC,5

    ServerD,2

    ServerE,2

    ServerF,4

    ServerG,1

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

    Note:

    * 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
    Profile photo of Sikander Mirza
    Sikander Mirza
    Participant

    Hi,
    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 ServerA.xyz.com, ServerB.xyz.com. [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
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    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.