ADD-ADGroupMember at Random in 4 Groups from list of computers, with no repeats/duplicates between 4 groups

135 Views Asked by At

My code below is not able to take the " $groupname " list and run it in the ADD-ADGroupmember step. I tried a different way, but I still end up with a computer showing in different groups. All I want to do is add computers to a select number of groups at Random, and only allow each unique computer to be assigned to one group? Any ideas?

$logfile = "results-$((Get-Date).ToString('MM-dd-yyyy_hhmmtt')).log"

Get-Date | Out-File $logfile

$groupnameprefix = "Automated"

$organizationalunitpath="OU paths go here. There are multiple"


$grouporganizationalunitpath = "OU paths for AD Security Groups go here"
$NumberofComputersPerGroup = "300"
# This is a list of computer names that will be used to generate group names
$Patch_Groups = @(
"_Group_1",
"_Group_2",
"_Group_3",
"_Group_4"
)

# Get all computers from the specified organizational unit
[System.Collections.Generic.List[object]] $computers = $organizationalunitpath |
    ForEach-Object { Get-ADComputer -Filter * -SearchBase $_ }

# Create the new group
foreach ($Patch_Group in $Patch_Groups){
# Generate the group name by concatenating the prefix and the Patch Group name with spaces removed
$groupname = $groupnameprefix + $Patch_Group.Replace(" ","")
try {
# Create the group using the generated name and the specified organizational unit path
New-ADGroup -Name $groupName -path $grouporganizationalunitpath -GroupScope Global -verbose
} 

catch { 
         $message = "ADGroup $groupName already exists"
            $message | Out-File $logfile -Append
            Write-Warning $message
            Write-Warning $_.Exception.Message
            $_.Exception.Message | Out-File $logfile -Append
 }


    try {
        $AddComputerstoRandomADGroups = Get-Random -input $computers -count 300
        $computers = $computers | where {$_ -notin $AddComputerstoRandomADGroups}
        Add-ADGroupMember -Identity $groupname -Members $AddComputerstoRandomADGroups -verbose
    
    
    }
    catch {
        $message = ' A problem occurred trying to add Members'
        $message | Out-File $logfile -Append
        Write-Warning $message
        Write-Warning $_.Exception.Message
        $_.Exception.Message | Out-File $logfile -Append
    }
}
1

There are 1 best solutions below

11
Keith Langmead On

OK, ignore what I wrote previously, I see what's happening now.

The Get-Random bit of your code isn't selecting a random Computer, it's just picking a random number and passing that down the pipeline.

Assuming $computers is no more complex than the equivalent of :

$computers=@()
$computers="computer1","computer2","computer3","computer4","computer5","computer6","computer7","computer8"

then this code will do the trick :

$AddComputerstoRandomADGroups = Get-Random -input $computers -count 4
$computers = $computers | where {$_ -notin $AddComputerstoRandomADGroups}
Add-ADGroupMember -Identity $groupname -Members $AddComputerstoRandomADGroups -verbose

which in this instance, selects 4 computers at random from the 8 currently in $computers, then updates the $computers variable to equal everything in $computers EXCEPT those computers assigned to $AddComputerstoRandomADGroups so they don't get selected in future iterations.

Edit - if you're still not finding the script works I'd suggest you need to output what is actually happening to narrow down where your issue is. As an example, I took your code and stripped it down to the following, which works fine.

$organizationalunitpath="CN=Computers,DC=mydomain"
$Patch_Groups = @(
"_Group_1",
"_Group_2",
"_Group_3",
"_Group_4"
)
$groupnameprefix = "Automated"
[System.Collections.Generic.List[object]] $computers = $organizationalunitpath |
    ForEach-Object { Get-ADComputer -Filter * -SearchBase $_ }
foreach ($Patch_Group in $Patch_Groups){
    $groupname = $groupnameprefix + $Patch_Group.Replace(" ","")
   # write-host "New Group - Groupname = $groupname - path = $oupath"
    $AddComputerstoRandomADGroups = Get-Random -input $computers -count 20
    $computers = $computers | where {$_ -notin $AddComputerstoRandomADGroups}
   # write-host "add to $groupname computer $AddComputerstoRandomADGroups"   
   write-host "-- computers"
   $computers.name 
   write-host "-- added computers"
   $AddComputerstoRandomADGroups.name 
   write-host "---"
}

Throughout each of the four iterations the number of entries within the $computers variable is reducing, so the specified number of machines are being selected at random, potentially added to the group, and then those computers are being removed from that list so they're no longer available to be returned, which you can also see from the output for $AddComputerstoRandomADGroups and I'm not seeing any machines multiple times in the four outputs for $AddComputerstoRandomADGroups.

So try running that and see what output you get, since you can try it and see what's going to be happening without actually creating groups and adding machines to them.