Get-NestedDLMembers.ps1
<#PSScriptInfo
.VERSION 1.0 .GUID 2b07a799-970b-4ddf-bcfe-2fa5033a663f .AUTHOR David Dean <david@sycho.net> .DESCRIPTION Have a distribution list with a bunch of nested groups? Want to get a flattened member list? This is what you need. ################################################################################# # Get-NestedDLMembers.ps1 # # This will expand and flatten a Distribution List with nested groups into a # single global array $Members. By default it will only output the # PrimarySmtpAddress but additional properies can be added with the -properties # switch (Note that the properties must be quoted and comma separated. By default # it pulls all RecipientTypes which can be narrowed to a single RecipientType # with the -UserType switch (UserMailbox, MailUser or MailContact). # # Syntax: # Get-NestedDLMembers.ps1 -DLName <DistributionList> -Properties # "CustomAttribute1","CustomAttribute2","RecipientType" # # Note that member deduplication is done by default. # # # ################################################################################# # # Created by David Dean <david@sycho.net> # ################################################################################# #> param ( [Parameter(ValuefromPipeline=$true,mandatory=$true)][String] $DLName, [Parameter(mandatory=$false)][String] $UserType="All", [Parameter(mandatory=$false)]$Properties=@() ) if ($UserType -eq $null) { $UserType = "Missing" } switch($UserType) { "Mailbox"{ $UserType = "UserMailbox" } "MailUser"{ $UserType = "MailUser" } "All"{ $UserType = "All" } } if (($Properties) -and ($Properties -notcontains "PrimarySmtpAddress")) { $tempProps = $Properties [System.Collections.ArrayList]$Properties = @() $tempProps |%{$Properties.add($_) |out-null} $Properties.add("PrimarySmtpAddress") |out-null } else { [System.Collections.ArrayList]$Properties = @() $Properties.add("PrimarySmtpAddress") |out-null } [System.Collections.ArrayList]$Global:Members = @() $DLInfo = $null $DLMemberInfo = $null $DLInfo = Get-DistributionGroup $DLName $DLMemberInfo = Get-DistributionGroupMember $DLName -ResultSize Unlimited write-host "Unpacking group: $($DLInfo.name)" [System.Collections.ArrayList]$NestedGroup = @($DLMemberInfo |?{($_.RecipientType -eq "MailUniversalSecurityGroup") -or ($_.RecipientType -eq "MailUniversalDistributionGroup")}) if ($UserType -ne "All") { if ($DLMemberInfo.Recipienttype -eq $UserType) { $DLMemberInfo |?{($_.RecipientType -eq $UserType)} |select $Properties |%{if ("$(($Global:Members).PrimarySmtpAddress)" -notmatch "$(($_).PrimarySmtpAddress)") {$Global:Members.add($_) |out-null}} } } else { $DLMemberInfo |?{($_.RecipientType -notlike "MailUniversal*")} |select $Properties |%{if ("$(($Global:Members).PrimarySmtpAddress)" -notmatch "$(($_).PrimarySmtpAddress)") {$Global:Members.add($_) |out-null}} } do { [System.Collections.ArrayList]$TempNested = @() foreach ($Group in $NestedGroup) { $NestedDLInfo = Get-DistributionGroup $Group.name write-host "Unpacking group: $($NestedDLInfo.name)" $NestedDLMemberInfo = Get-DistributionGroupMember $Group.name -ResultSize Unlimited $NestedDLMemberInfo |?{($_.RecipientType -eq "MailUniversalSecurityGroup") -or ($_.RecipientType -eq "MailUniversalDistributionGroup")} |%{$TempNested.add($_) |out-null} if ($UserType -ne "All") { if ($NestedDLMemberInfo.Recipienttype -eq $UserType) { $NestedDLMemberInfo |?{($_.RecipientType -eq $UserType)} |select $Properties |%{if ("$(($Global:Members).PrimarySmtpAddress)" -notmatch "$(($_).PrimarySmtpAddress)") {$Global:Members.add($_) |out-null}} } } else { $NestedDLMemberInfo |?{($_.RecipientType -notlike "MailUniversal*")} |select $Properties |%{if ("$(($Global:Members).PrimarySmtpAddress)" -notmatch "$(($_).PrimarySmtpAddress)") {$Global:Members.add($_) |out-null}} } } [System.Collections.ArrayList]$NestedGroup = @() if ($TempNested) {$TempNested |%{$NestedGroup.add($_) |out-null}} } while ($NestedGroup.count -gt 0) |