I'm working on AD nested groups and need some help.
Here's the code:
function Extract-Users($group, $array) {
$members = Get-ADGroupMember -Identity $group.Name
foreach ($member in $members) {
if ($member.objectClass -eq "user") {
$Admin = New-Object PSObject
$Admin | Add-Member -Type NoteProperty -Name User -Value $user.Name
$Admin | Add-Member -Type NoteProperty -Name Group -Value $group.Name
$array += $Admin
Remove-Variable Admin
return $array
} elseif ($member.objectClass -eq "group"){
Extract-Users $member, $object
}
}
}
The issue is that I want to be able to call the function like this:
$ADGroups = Get-ADGroup -Filter {Name -like "*admin*"}
$Admins = @()
foreach ($group in $ADGroups) {
Extract-Users $group $Admins
}
And store all the objects that are created in the function into the variable that I'm tossing into the function. So that all the users that I extract are stored into the $Admins array. I know I can write it all manual, but I want to know if there is a dynamic way of doing it.
Thanks guys