diff --git a/Office 365 Distribution Group Members Report/GetDistributionGroupMembers.ps1 b/Office 365 Distribution Group Members Report/GetDistributionGroupMembers.ps1 index f1a99a6..5fc9c5b 100644 --- a/Office 365 Distribution Group Members Report/GetDistributionGroupMembers.ps1 +++ b/Office 365 Distribution Group Members Report/GetDistributionGroupMembers.ps1 @@ -1,7 +1,7 @@ <# ============================================================================================= Name: Get distribution group members report -Version: 3.0 +Version: 4.0 Website: o365reports.com Script Highlights: @@ -20,6 +20,16 @@ Script Highlights: 11.The script is scheduler friendly. i.e., credentials can be passed as parameter instead of saving inside the script. 12.Above all, script exports output in nicely formatted 2 CSV files. One with detailed information and another with summary information. + +Change Log: +V1.0 (Nov 01, 2019)- Script created +V2.0 (Dec 13, 2022)- Upgraded EXO module. Now, you can connect to Exchange Online PowerShell using modern authentication +V3.0 (Oct 06, 2023)- Usability improvements +V4.0 (Jan 27, 2025) - Due to MS update, ManagedBy and Member Names are shown as ID. Handled code for showing user-identifiable name instead of Ids. + Added support for certificate-based authentication(CBA) + + + For detailed script execution: https://o365reports.com/2019/05/23/export-office-365-distribution-group-members-csv/ ============================================================================================ #> @@ -31,7 +41,10 @@ Param [int]$MinGroupMembersCount, [Nullable[boolean]]$ExternalSendersBlocked = $null, [string]$UserName, - [string]$Password + [string]$Password, + [string]$Organization, + [string]$ClientId, + [string]$CertificateThumbprint ) Function Get_members @@ -70,17 +83,28 @@ Function Get_members $AuthorizedSenders="Senders inside & Outside of Your Organization" } - $Manager="" + $Manager = @() if($_.ManagedBy.Count -gt 0) { foreach($ManageBy in $ManagedBy) - { - $Manager=$Manager+$ManageBy - if($ManagedBy.indexof($ManageBy) -lt (($ManagedBy.count)-1)) + { #Verify if manager property returned as ID and convert it to user-identifiable name + if($ManageBy -match '(?im)^[{(]?[0-9A-F]{8}[-]?(?:[0-9A-F]{4}[-]?){3}[0-9A-F]{12}[)}]?$') { - $Manager=$Manager+"," + if($ManagerHash.ContainsKey($ManageBy)) + { + $ManagerName=$ManagerHash[$ManageBy] + } + # Retrieve the display name for the ID + else + { + $ManagerName=(Get-EXORecipient -Identity $ManageBy).DisplayName + $ManagerHash[$ManageBy]=$ManagerName + } + $ManageBy=$ManagerName } + $Manager+=$ManageBy } + $Manager= $Manager -join "," } $Recipient="" $RecipientHash=@{} @@ -120,6 +144,8 @@ Function Get_members } $RecipientTypeDetail=$Member.RecipientTypeDetails $MemberEmail=$Member.PrimarySMTPAddress + $MemberName=$Member.DisplayName + if($MemberEmail -eq "") { $MemberEmail="-" @@ -163,7 +189,7 @@ Function Print_Output { if($Print -eq 1) { - $Result=@{'DisplayName'=$DisplayName;'PrimarySmtpAddress'=$EmailAddress;'Alias'=$Alias;'Members'=$Member;'MemberEmail'=$MemberEmail;'MemberType'=$RecipientTypeDetail} + $Result=@{'DisplayName'=$DisplayName;'PrimarySmtpAddress'=$EmailAddress;'Alias'=$Alias;'Members'=$MemberName;'MemberEmail'=$MemberEmail;'MemberType'=$RecipientTypeDetail} $Results= New-Object PSObject -Property $Result $Results | Select-Object DisplayName,PrimarySmtpAddress,Alias,Members,MemberEmail,MemberType | Export-Csv -Path $ExportCSV -Notype -Append } @@ -172,24 +198,21 @@ Function Print_Output Function main() { - #Clean up session - Get-PSSession | Remove-PSSession - #Check for EXO v2 module inatallation + #Check for EXO module inatallation $Module = Get-Module ExchangeOnlineManagement -ListAvailable if($Module.count -eq 0) { - Write-Host Exchange Online PowerShell V2 module is not available -ForegroundColor yellow + Write-Host Exchange Online PowerShell module is not available -ForegroundColor yellow $Confirm= Read-Host Are you sure you want to install module? [Y] Yes [N] No if($Confirm -match "[yY]") { Write-host "Installing Exchange Online PowerShell module" Install-Module ExchangeOnlineManagement -Repository PSGallery -AllowClobber -Force - Import-Module ExchangeOnlineManagement } else { - Write-Host EXO V2 module is required to connect Exchange Online.Please install module using Install-Module ExchangeOnlineManagement cmdlet. + Write-Host EXO module is required to connect Exchange Online.Please install module using Install-Module ExchangeOnlineManagement cmdlet. Exit } } @@ -199,12 +222,17 @@ Function main() { $SecuredPassword = ConvertTo-SecureString -AsPlainText $Password -Force $Credential = New-Object System.Management.Automation.PSCredential $UserName,$SecuredPassword - Connect-ExchangeOnline -Credential $Credential + Connect-ExchangeOnline -Credential $Credential -ShowBanner:$false + } + elseif($Organization -ne "" -and $ClientId -ne "" -and $CertificateThumbprint -ne "") + { + Connect-ExchangeOnline -AppId $ClientId -CertificateThumbprint $CertificateThumbprint -Organization $Organization -ShowBanner:$false } else { - Connect-ExchangeOnline + Connect-ExchangeOnline -ShowBanner:$false } + #Set output file $ExportCSV=".\DistributionGroup-DetailedMembersReport_$((Get-Date -format yyyy-MMM-dd-ddd` hh-mm` tt).ToString()).csv" #Detailed report @@ -214,7 +242,7 @@ Function main() $RecipientTypeArray=Get-Content -Path .\RecipientTypeDetails.txt -ErrorAction Stop $Result="" $Results=@() - + $ManagerHash = @{} #Check for input file if([string]$GroupNamesFile -ne "") {