A cloud-based service included in Microsoft 365, delivering scalable messaging and collaboration features with simplified management and automatic updates.
Hi @Rising Flight ,
Welcome to the Microsoft Q&A platform!
Based on your description, your script looks well-structured! Here are some suggestions to ensure it runs smoothly:
- Make sure you have installed and imported the necessary modules, such as ExchangeOnlineManagement.
- Add error handling to manage any unexpected issues.
- Test the script with a small subset of data to verify its functionality before running it on the entire dataset.
Here is a slightly improved version of your script with error handling added:
# Import the CSV file containing unified group names
$UGList = Import-Csv -Path "c:\temp\input.csv"
if ($UGList.Count -eq 0) {
Write-Host "No data found in the CSV file."
exit
}
$UnifiedGroupsWithExternalUsers = @()
$totalUGs = $UGList.Count
$currentUG = 0
foreach ($UG in $UGList) {
$currentUG++
Write-Progress -Activity "Processing Distribution Groups" -Status "$currentUG out of $totalUGs" -PercentComplete (($currentUG / $totalUGs) * 100)
try {
# Fetch the Unified Group
$Group = Get-UnifiedGroup -Identity $UG.UGAlias -ResultSize Unlimited
if ($Group) {
# Fetch the members and filter external users
$Members = Get-UnifiedGroupLinks -Identity $Group.Identity -LinkType Members -ResultSize Unlimited | Where-Object {
$_.RecipientType -eq 'MailContact' -or $_.RecipientType -eq 'MailUser' -or $_.ExternalEmailAddress -ne $null
}
if ($Members) {
foreach ($Member in $Members) {
$UnifiedGroupsWithExternalUsers += [PSCustomObject]@{
GroupName = $Group.DisplayName
MemberName = $Member.DisplayName
MemberType = $Member.RecipientType
MemberEmail = $Member.PrimarySmtpAddress
}
}
}
} else {
Write-Host "Warning: Unified Group '$($UG.UGAlias)' not found."
}
} catch {
Write-Host "Error processing group '$($UG.UGAlias)': $_"
}
}
# Export the results to a CSV file
$UnifiedGroupsWithExternalUsers | Export-Csv -Path "C:\temp\output.csv" -NoTypeInformation
Write-Host "Export completed. File saved to C:\temp\output.csv"
Please feel free to contact me for any updates. And if this helps, don't forget to mark it as an answer.
Best,
Jake Zhang