i would like to extract members from an AD Group that contains Members and security group.
Example, Group_A: User1 User2 User3 Group_B
When I run my script, it shows:
CN=User1,OU=Users,DC=Contoso,DC=com
CN=User2,OU=Users,DC=Contoso,DC=com
CN=User3,OU=Users,DC=Contoso,DC=com
CN=Group_B,OU=Users,DC=Contoso,DC=com
Is there another way to show their Name and/or SamAccountname?
$Groups =
@"
GroupNames;
Group_A
"@ | ConvertFrom-Csv -Delimiter ';'
$ADGroups =
Foreach ($Group in $Groups){
Get-ADGroup $Group.GroupNames -Server contoso.com -Properties Members }
$ADGroups.Members
>Solution :
If you want to play safe, you can pipe the result of Get-ADGroup to Get-ADGroupMember, this would also be helpful because you would be able to disintguish the ObjectClass of each member:
$ADGroups = foreach ($Group in $Groups) {
Get-ADGroup $Group.GroupNames -Server contoso.com |
Get-ADGroupMember -Server contoso.com | Select-Object SamAccountName, ObjectClass
}
You could also do string manipulation over the elements of the member array by following this Q&A.