counting an array 10 at a time until complete

Van Thillo, James 21 Reputation points
2020-08-03T20:55:27.81+00:00

Hello,

How do you loop through an array 10 at a time until complete? I would like to reboot quite a few servers in a cluster and have come up with the following but I am curious is there is a way get 10, then get the next 10 and the next 10, etc until finished. The cluster can grow later so I am trying to build something a bit more dynamic.

foreach ($server in $pascal[0..9]){write-host $server is rebooting }
Start-Sleep -Seconds 30
foreach ($server in $pascal[10..19]){write-host $server is rebooting }
Start-Sleep -Seconds 30
foreach ($server in $pascal[20..29]){write-host $server is rebooting }
Start-Sleep -Seconds 30
foreach ($server in $pascal[30..39]){write-host $server is rebooting }
Start-Sleep -Seconds 30
foreach ($server in $pascal[40..49]){write-host $server is rebooting }
Start-Sleep -Seconds 30
foreach ($server in $pascal[50..59]){write-host $server is rebooting }
Start-Sleep -Seconds 30
foreach ($server in $pascal[60..69]){write-host $server is rebooting }
Start-Sleep -Seconds 30

etc..

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,488 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 46,076 Reputation points
    2020-08-03T21:28:34.63+00:00

    You could try this:

    $count = 1
      $pascal |
        ForEach-Object{
          Restart-Computer $_
          $Count++
          If ($Count -gt 10){
            Start-Sleep -Seconds 30
            $Count = 1
        }
    

    It doesn't deal with an array, as such, but it does deal with the dynamism you asked for, and it doesn't have to deal with things like having the number of machines in the $pascal array not being a multiple of 10


0 additional answers

Sort by: Most helpful