Select users in groups distinct bunches

Welcome Forums General PowerShell Q&A Select users in groups distinct bunches

Viewing 4 reply threads
  • Author
    Posts
    • #196814
      Participant
      Topics: 17
      Replies: 16
      Points: 45
      Rank: Member

      I would like to go through a large list of users but process them in batches of about 15.  For each of the distinct group of 15, they would get a distinct accountexpirationdate e.g., “Set-ADUser -AccountExpirationDate $StartDate.AddDays($j)” where $j is the certain batch of 15.  So, for example, I have 1500 total users and I would like to take each 15 and give them an accountexpirationdate of say March 1, 2020 and then the next batch of 15 and increment the accountexpirationdate by one day ie.., March 2, 2020 and so on.

      I can do this by placing a for loop going through all 1500 users and then nesting a bunch of if statements (about 100) that would do the trick. I am looking for a more clever and concise bit of scripting.

    • #196847
      Participant
      Topics: 5
      Replies: 328
      Points: 521
      Helping Hand
      Rank: Major Contributor

      You should provide some code first, but below is a foreach loop example that will work.

    • #196856
      Participant
      Topics: 13
      Replies: 1753
      Points: 3,136
      Helping Hand
      Rank: Community Hero

      As js said, you should provide some code on what you’ve tried. Something to start with:

    • #196859
      Participant
      Topics: 17
      Replies: 16
      Points: 45
      Rank: Member

      @random command line.  I don’t quite follow your script. Here is what I had in my not-so-well attempt to consolidate:

      $Consults = $UserTot | where {($_.’User Class’ -eq “Consultant” -or $_.’User Class’ -eq “Contractor”}

      $StartDate = Get-Date 03/01/2020
      $totincrements = [int][Math]::Ceiling($Consults.count / 15)

      for ($j=0; $j -le $totincrements; $j++) {
      $multiplier = $j*15
      if ($j -eq 0) {
      $smallbatch = $Consults.samAccountName | Select -First 15
      }
      else {
      $smallbatch = $Consults.samAccountName | Select -Skip $multiplier
      }
      Write-Host “The multiplier is $($multiplier)” -ForegroundColor Yellow
      Write-Host “The $($j) batch`n`nHas the following amount:$($smallbatch.count)”
      #$smallbatch | %{Set-ADUser -AccountExpirationDate $StartDate.AddDays($j) -Credential $credINT}
      }

      In the first run, it will set the first 15 consultants to have an expiration date of March 1st.  On the second run, it will set all but the first 15 in $Consult to March 2, 2020 (total of 1500-15=1485 users).  Then on the third run, it will leave users 16-30 at March 2nd but set the rest of the 1470 users to March 3rd and so on.  I think it will do the trick, but not really “tight” nor clean.

    • #196865
      Participant
      Topics: 17
      Replies: 16
      Points: 45
      Rank: Member

      Ah, I did not know you could combine both -First and -Skip switches within the same select statement.  So, my final code should work:

      for ($j=0; $j -le $totincrements; $j++) {
      $multiplier = $j*15
      if ($j -eq 0) {
      $smallbatch = $Consults.samAccountName | Select -First 15
      }
      else {
      $smallbatch = $Consults.samAccountName | Select -First 15 -Skip $multiplier
      }
      Write-Host “The multiplier is $($multiplier)” -ForegroundColor Yellow
      Write-Host “The $($j) batch`n`nHas the following users:$($smallbatch)”
      #$smallbatch | %{Set-ADUser -AccountExpirationDate $StartDate.AddDays($j) -Credential $credINT}
      }

       

      Thank you, very much!

Viewing 4 reply threads
  • The topic ‘Select users in groups distinct bunches’ is closed to new replies.