Updated to show user-identifiable name in Member Names and ManagedBy due to recent MS update

This commit is contained in:
AdminDroid 2025-01-27 14:39:21 +05:30
parent 112edd6d14
commit 019217763f

View File

@ -1,7 +1,7 @@
<# <#
============================================================================================= =============================================================================================
Name: Get distribution group members report Name: Get distribution group members report
Version: 3.0 Version: 4.0
Website: o365reports.com Website: o365reports.com
Script Highlights: 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. 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. 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/ For detailed script execution: https://o365reports.com/2019/05/23/export-office-365-distribution-group-members-csv/
============================================================================================ ============================================================================================
#> #>
@ -31,7 +41,10 @@ Param
[int]$MinGroupMembersCount, [int]$MinGroupMembersCount,
[Nullable[boolean]]$ExternalSendersBlocked = $null, [Nullable[boolean]]$ExternalSendersBlocked = $null,
[string]$UserName, [string]$UserName,
[string]$Password [string]$Password,
[string]$Organization,
[string]$ClientId,
[string]$CertificateThumbprint
) )
Function Get_members Function Get_members
@ -70,17 +83,28 @@ Function Get_members
$AuthorizedSenders="Senders inside & Outside of Your Organization" $AuthorizedSenders="Senders inside & Outside of Your Organization"
} }
$Manager="" $Manager = @()
if($_.ManagedBy.Count -gt 0) if($_.ManagedBy.Count -gt 0)
{ {
foreach($ManageBy in $ManagedBy) foreach($ManageBy in $ManagedBy)
{ { #Verify if manager property returned as ID and convert it to user-identifiable name
$Manager=$Manager+$ManageBy if($ManageBy -match '(?im)^[{(]?[0-9A-F]{8}[-]?(?:[0-9A-F]{4}[-]?){3}[0-9A-F]{12}[)}]?$')
if($ManagedBy.indexof($ManageBy) -lt (($ManagedBy.count)-1))
{ {
$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="" $Recipient=""
$RecipientHash=@{} $RecipientHash=@{}
@ -120,6 +144,8 @@ Function Get_members
} }
$RecipientTypeDetail=$Member.RecipientTypeDetails $RecipientTypeDetail=$Member.RecipientTypeDetails
$MemberEmail=$Member.PrimarySMTPAddress $MemberEmail=$Member.PrimarySMTPAddress
$MemberName=$Member.DisplayName
if($MemberEmail -eq "") if($MemberEmail -eq "")
{ {
$MemberEmail="-" $MemberEmail="-"
@ -163,7 +189,7 @@ Function Print_Output
{ {
if($Print -eq 1) 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= New-Object PSObject -Property $Result
$Results | Select-Object DisplayName,PrimarySmtpAddress,Alias,Members,MemberEmail,MemberType | Export-Csv -Path $ExportCSV -Notype -Append $Results | Select-Object DisplayName,PrimarySmtpAddress,Alias,Members,MemberEmail,MemberType | Export-Csv -Path $ExportCSV -Notype -Append
} }
@ -172,24 +198,21 @@ Function Print_Output
Function main() 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 $Module = Get-Module ExchangeOnlineManagement -ListAvailable
if($Module.count -eq 0) 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 $Confirm= Read-Host Are you sure you want to install module? [Y] Yes [N] No
if($Confirm -match "[yY]") if($Confirm -match "[yY]")
{ {
Write-host "Installing Exchange Online PowerShell module" Write-host "Installing Exchange Online PowerShell module"
Install-Module ExchangeOnlineManagement -Repository PSGallery -AllowClobber -Force Install-Module ExchangeOnlineManagement -Repository PSGallery -AllowClobber -Force
Import-Module ExchangeOnlineManagement
} }
else 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 Exit
} }
} }
@ -199,13 +222,18 @@ Function main()
{ {
$SecuredPassword = ConvertTo-SecureString -AsPlainText $Password -Force $SecuredPassword = ConvertTo-SecureString -AsPlainText $Password -Force
$Credential = New-Object System.Management.Automation.PSCredential $UserName,$SecuredPassword $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 else
{ {
Connect-ExchangeOnline Connect-ExchangeOnline -ShowBanner:$false
} }
#Set output file #Set output file
$ExportCSV=".\DistributionGroup-DetailedMembersReport_$((Get-Date -format yyyy-MMM-dd-ddd` hh-mm` tt).ToString()).csv" #Detailed report $ExportCSV=".\DistributionGroup-DetailedMembersReport_$((Get-Date -format yyyy-MMM-dd-ddd` hh-mm` tt).ToString()).csv" #Detailed report
$ExportSummaryCSV=".\DistributionGroup-SummaryReport_$((Get-Date -format yyyy-MMM-dd-ddd` hh-mm` tt).ToString()).csv" #Summary report $ExportSummaryCSV=".\DistributionGroup-SummaryReport_$((Get-Date -format yyyy-MMM-dd-ddd` hh-mm` tt).ToString()).csv" #Summary report
@ -214,7 +242,7 @@ Function main()
$RecipientTypeArray=Get-Content -Path .\RecipientTypeDetails.txt -ErrorAction Stop $RecipientTypeArray=Get-Content -Path .\RecipientTypeDetails.txt -ErrorAction Stop
$Result="" $Result=""
$Results=@() $Results=@()
$ManagerHash = @{}
#Check for input file #Check for input file
if([string]$GroupNamesFile -ne "") if([string]$GroupNamesFile -ne "")
{ {