It's easy to get a list of all members of a Distribution Group. The Exchange shell (
EMS) ships with the
Get-DistributionGroupMember cmdlet that makes it a short one-liner
(compared to 100s of lines of code in VBS).
However, how do we get all Distribution Groups a user, group, or contact is a member of? There's no equivalent cmdlet that can list a recipient's distribution group memberships using the shell. From the AD side, a recipient's
memberOf attribute is a back-linked attribute, which I briefly talked about in
memberOf Attribute can now be used in OPATH filters!. A group's membership is stored in the group's
member attribute.
In the following command/script
(what's the boundary between a command and a script?? when do a bunch of commands become a script?), we look at all distribution groups in AD, look at each member and determine if it matches the one we're looking for.
$contact = get-contact "foo@somedomain.com"; Get-DistributionGroup | foreach {$dg = $_ ; write-host "Looking at: "
$dg; Get-DistributionGroupMember $dg | foreach {if ($_.identity -like $contact.identity) {"Member of : " + $dg} }}
Clearly, this isn't very efficient!
Using the ADSI providerThe shell can also look at the AD objects
natively using the
ADSI provider. It's not as friendly or easy to use
(as a native AD provider for Powershell would probably be), but it's a huge improvement over VBScript. There's no need to grab AD objects into
ADO recordsets— that part is taken care of by Powershell.
Here's one way to do this using the ADSI provider:
$dn = "LDAP://" + (Get-Contact foo@somedomain.com).distinguishedName; $foo=[ADSI]$dn; $foo.memberOf | foreach {$dg = $_; get-distributiongroup $dg}
Here's a script with some changes and validation:
Get-DGMembership.zipWhat it does: Uses the ADSI provider to get list of all groups a recipient is a member of, determines if the group is a Distribution or Security group, outputs names of Distribution Groups.
Usage: .\Get-DGMembership.ps1 Mailbox1@mydomain.com
.\Get-DGMembership.ps1 Mailbox1@mydomain.com Contact2@somedomain.com
What we can really use is a native AD provider that lends the same automation capabilities to AD management tasks that the Exchange shell and Powershell lend to Exchange and Windows management tasks.
Labels: AD/LDAP, Administration, Exchange Server 2007, Exchange Shell, Scripting, Scripts