Cycling though an array in increments of 15

Welcome Forums General PowerShell Q&A Cycling though an array in increments of 15

Viewing 2 reply threads
  • Author
    Posts
    • #188845
      Participant
      Topics: 15
      Replies: 13
      Points: 16
      Rank: Member

      I am trying to cycle through an array (converted to an arraylist) that has over 790 users (variable $Consults).  I am trying to take it in batches of 15 users each (and eventually perform an action on each batch).  The $SubDone seems fine, it eventually accumulates all the users.  I get these weird numbers and it them makes  batch of 30 elements composed of 15 actually users (and it goes through all 790 nicely) but I cannot explain where these numbers come from.  Can someone shed some light on where these numbers arise and how to eliminate?

      $UserTot = Import-Csv -Path "C:\Users\admin11912\Documents\AD Reports\AD-User-Export-2019-11-15T11-48-12.csv"
      $Consults = $UserTot | where {($_.'User Class' -eq "Consultant" -or $_.'User Class' -eq "Contractor" -or $_.'User Class' -eq "Remote-Contractor" -or $_.'User Class' -eq "Vendor/Consultant" -or $_.'User Class' -eq "Vendor" -or $_.'User Class' -eq "Third Party Contractor" -or $_.'User Class' -eq "Partner" -or $_.'User Class' -eq "SOW Contractor") -and $_.accountexpirationdate.length -lt 1 -and $_.enabled -eq $true}
      
      [System.Collections.ArrayList]$SubConsults = $Consults.samAccountName | Select -First 15
      $totincrements = [int][Math]::Ceiling($Consults.count / 15)
      for ($i=1; $i -le $totincrements; $i++) {
      if ($i -eq 1) {
      Write-Host "Here are the first 15" -ForegroundColor Cyan
      $SubDone = $SubConsults
      Write-Host "Here are the subdone"
      $SubDone
      Write-Host "Here are the subconsults"
      $SubConsults
      }
      else {
      $SubConsults = ($Consults.samAccountName | Select -First ($i * 15))
      foreach ($subitem in $SubDone) {
      $SubConsults.Remove($subitem)
      }
      foreach ($subC in $SubConsults) {
      $SubDone.Add($subC)
      }
      Write-Host "Here is the $($i) batch of users" -foregroundcolor Yellow
      $SubConsults
      }
      } #end for loop
      
      

      The results look weird with these numbers where I do not know from whence they come (showing two batch iterations below). Note, the "User" is a valid user name in our system and hidden for privacy.  The number actually appears in the output:

      Here is the 25 batch of users
      User
      User
      User
      User
      User
      User
      User
      User
      User
      User
      User
      User
      User
      User
      User
      375
      376
      377
      378
      379
      380
      381
      382
      383
      384
      385
      386
      387
      388
      389
      Here is the 26 batch of users
      User
      User
      User
      User
      User
      User
      User
      User
      User
      User
      User
      User
      User
      User
      User
      390
      391
      392
      393
      394
      395
      396
      397
      398
      399
      400
      401
      402
      403
      404
      
    • #188908
      Participant
      Topics: 0
      Replies: 21
      Points: 136
      Helping Hand
      Rank: Participant

      The .Add()method of [System.Collections.ArrayList] is echoing the index of the item just added. That is causing the output of numbers. You have to use the usual tricks for suppressing the output (assign to or output to null).

      Change the add line to:

      $null = $SubDone.Add($subC)

    • #189019
      Participant
      Topics: 2
      Replies: 511
      Points: 1,306
      Helping Hand
      Rank: Community Hero

      Alternately I'd just suggest not using ArrayList. The Microsoft docs page for that class states that it is not being developed and should not be used for new code; instead, use System.Collections.Generic.List[T]. In this instance, since you're working with SamAccountName which is usually a string value, you can exchange your arraylist type for [System.Collections.Generic.List[string]] and get about the same results, except without the pesky numbers being output from .Add(). 🙂

Viewing 2 reply threads
  • You must be logged in to reply to this topic.