Après avoir migré d’un Exchange OnPrem sur Office365, la question de lister les membres d’un groupe récursivement c’est posé.
Après quelques recherches, j’ai trouvé ce script que j’ai adapté pour inclure les groupes de sécurités à extension de messagerie en plus des groupes de distributions.
Source : https://github.com/lazywinadmin/PowerShell/blob/master/O365-GROUP-Get-DistributionGroupRecursive/O365-GROUP-Get-DistributionGroupRecursive.ps1
function Get-DistributionGroupMemberRecursive { <# .SYNOPSIS This script will list all the members (recursively) of a DistributionGroup .EXAMPLE Get-DistributionGroupMemberRecursive -Group TestGroupe -Verbose .NOTES Francois-Xavier Cat lazywinadmin.com @lazywinadmin #> [CmdletBinding()] PARAM ($Group) BEGIN { TRY { # Retrieve Group information Write-Verbose -Message "[BEGIN] Retrieving members of $Group" $GroupMembers = Get-DistributionGroupMember -Identity $Group -ErrorAction Stop -ErrorVariable ErrorBeginGetDistribMembers | Select-Object -Property Name, PrimarySMTPAddress, @{ Label = "Group"; Expression = { $Group } }, RecipientType } CATCH { if ($ErrorBeginGetDistribMembers) { Write-Warning -Message "[BEGIN] Issue while retrieving members of $Group" } $PSCmdlet.ThrowTerminatingError($_) } } PROCESS { FOREACH ($Member in $GroupMembers) { TRY { Write-Verbose -Message "[PROCESS] Member: $($member.name)" SWITCH ($Member.RecipientType) { "MailUniversalDistributionGroup" { # Member's type is Distribution Group, we need to find members of this object Get-DistributionGroupMemberRecursive -Group $($Member.name) | Select-Object -Property Name, PrimarySMTPAddress, @{ Label = "Group"; Expression = { $($Member.name) } }, RecipientType Write-Verbose -Message "[PROCESS] $($Member.name)" } "MailUniversalSecurityGroup" { # Member's type is Mail Enable Security Group, we need to find members of this object Get-DistributionGroupMemberRecursive -Group $($Member.name) | Select-Object -Property Name, PrimarySMTPAddress, @{ Label = "Group"; Expression = { $($Member.name) } }, RecipientType Write-Verbose -Message "[PROCESS] $($Member.name)" } "UserMailbox" { # Member's type is User, let's just output the data $Member | Select-Object -Property Name, PrimarySMTPAddress, @{ Label = "Group"; Expression = { $Group } } } } } CATCH { $PSCmdlet.ThrowTerminatingError($_) } } } END { Write-Verbose -message "[END] Done" } }
Pour lancer la fonction:
Import-Module ExchangeOnlineManagement Connect-ExchangeOnline -UserPrincipalName admin@domain.onmicrosoft.com -ShowProgress $true #executer le code ci-dessus de la fonction Get-DistributionGroupMemberRecursive puis Get-DistributionGroupMemberRecursive -Group GROUPNAME