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