Crash893
Crash893

Reputation: 11702

modify Powershell get names and emails from spesific groups

I have a powershell script that is working great but I want to make a more spesific version for a task thats getting run alot

    $Groups = Get-ADGroup -Properties * -Filter * -SearchBase "OU=Groups,DC=markonsolutions,DC=local" 
Foreach($G In $Groups)
{
    Write-Host $G.Name
    Write-Host "-------------"
    $G.Members
    Write-Host " "
}

currently (as it should) it gets all the groups and returns the group name followed by user information

example:

Markon University
-------------
CN=John Doe,OU=Staff,DC=markon,DC=local
CN=Eric Doe,OU=Staff,DC=markon,DC=local
CN=Diana Doe,OU=Staff,DC=markon,DC=local

What Im trying to do is have it return

Markon University
-------------
John Doe, [email protected]
Eric Doe, [email protected]
Diana Doe,[email protected]

But only for 3 specific groups in our AD structure (Division 0 Division 1 Division 2)

I've tried playing with the filter properties to no avail and I'm at a loss for how to get the properties of the user name in the format I need

any help would be greatly appreciated.

Upvotes: 0

Views: 178

Answers (1)

JonnyG
JonnyG

Reputation: 759

Group.Members is an array of Strings (DistinguishedName identifiers).

To get the AD properties for those members, you'll need to query AD using Get-ADUser, and then print out the specific properties you need. The UserPrincipalName property generally is the domain user's Email address.

$gr1 = get-adgroup "G_SomeName1" -Prop * -SearchBase "SomeADOULocation1";
$gr2 = get-adgroup "G_SomeName2" -Prop * -SearchBase "SomeADOULocation2";
$gr3 = get-adgroup "G_SomeName3" -Prop * -SearchBase "SomeADOULocation3";
write-host $Gr1.Name;
ForEach($member in $gr1.Members){
    $u=get-adobject -id $member -Prop *;
    If($u.ObjectClass -eq "user"){
        write-host "$($u.ObjectClass): $($u.name), $($u.UserPrincipalName)";
    }else{
        write-host "$($u.ObjectClass): $($u.Name), $($u.DistinguishedName)";
    }
}

write-host $Gr2.Name;
ForEach($member in $gr2.Members){
    $u=get-adobject -id $member -Prop *;
    If($u.ObjectClass -eq "user"){
        write-host "$($u.ObjectClass): $($u.name), $($u.UserPrincipalName)";
    }else{
        write-host "$($u.ObjectClass): $($u.Name), $($u.DistinguishedName)";
    }
}


write-host $Gr3.Name;
ForEach($member in $gr3.Members){
    $u=get-adobject -id $member -Prop *;
    If($u.ObjectClass -eq "user"){
        write-host "$($u.ObjectClass): $($u.name), $($u.UserPrincipalName)";
    }else{
        write-host "$($u.ObjectClass): $($u.Name), $($u.DistinguishedName)";
    }
}

Upvotes: 1

Related Questions