Author Posts

May 18, 2017 at 6:55 pm

Hi there
i am wondering what are my options in terms of away to devide 1000 users to 3 groups of users that i need to create diffrent tasks for.
all 1000 users are in a specific ou in active directory.
i know i can probably just query and export them to txt manually devide them to 3 txt files and then import each and do the task for that group.
seems kind of "manual" to me
i was wondering if someone has any other ideas ?

Thanks in advance

May 18, 2017 at 7:03 pm

It depends on whether you want to divide the users randomly, take 1-333, 334-666, 667-1000 or there is some criteria you're going to use to split up the users

May 18, 2017 at 9:02 pm

Thanks for your reply
i just need to divide them to 3 ,basically to divide the load on the servers im running the task on.
ill drop each "job/batch" on a different server
that's my intention
Thanks

May 19, 2017 at 6:27 am

This will work. May not be the best code.

Will group users based on the last number in their SID. Won't be perfectly balanced, but will work for your use case.

Get-ADUser -filter * | Group-Object -Property {$_.SID.Value.Substring(43,1) -le 3},{$_.SID.Value.Substring(43,1) -gt 3 -and $_.SID.Value.Substring(43,1) -le 6},{$_.SID.Value.Substring(43,1) -ge 7}

May 19, 2017 at 4:56 pm

If you're looking for a "Round Robin" solution then you could use a counter.
E.g.

$users = # you need to populate this with e.g. Get-ADUser -Filter * -SearchBase "The DN of the OU"

$counter = 1

foreach($u in $users)
{
 Switch($counter)
 {
  1 { Add-ADGroupMember -Identity "The First Group" -Members $u.SamAccountName; $counter++; break;}
  2 { Add-ADGroupMember -Identity "The Second Group" -Members $u.SamAccountName; $counter++; break;}
  3 { Add-ADGroupMember -Identity "The Third Group" -Members $u.SamAccountName; $counter = 1; break;}
 }
}

Just replace Add-ADGroupMember with whatever you want to do with them.
Just used that as an example.