查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
在 Exchange 组织或 Exchange Online (Microsoft 365) 租户中,您可以创建数百个通讯组(通讯组列表,DL)。 Exchange 管理员应定期删除不再使用的通讯组列表。在本文中,我们将向您展示如何查找并删除 Exchange 中未使用的和空的通讯组。
内容:
- 使用 PowerShell 列出 Exchange 中的空通讯组
- 如何在 Exchange Server 中查找不活动的通讯组
- 识别 Microsoft 365 (Exchange Online) 中的非活动通讯组列表
查找未使用的通讯组列表与查找不活动的 AD 用户或计算机不同。 Exchange DL 没有像 LastLogonDate / LastLogonTimeStamp 这样的属性来跟踪上次使用对象的时间。您可以使用 Exchange 跟踪日志来确定电子邮件是否已发送到特定通讯组。
使用 PowerShell 列出 Exchange 中的空通讯组
如果通讯组中没有用户,则很可能不再需要它。
您可以使用 PowerShell 在 Exchange 组织中查找空通讯组。使用 PowerShell 连接到您的 Exchange Server 并运行以下一行:
Get-DistributionGroup –ResultSize Unlimited |Where-Object { (Get-DistributionGroupMember –Identity $_.Name –ResultSize Unlimited).Count -eq 0} | select Name, PrimarySmtpAddress

分析生成的通讯组列表并使用以下命令删除(隐藏)不需要的 DLRemove-DistributionGroup命令。
同样,您可以使用 Get-DynamicDistributionGroup 查找空的动态 DL:
Get-DynamicDistributionGroup -ResultSize Unlimited | Where-Object { (Get-Recipient -RecipientPreviewFilter (Get-DynamicDistributionGroup -Identity $_.Identity).RecipientFilter).count -eq 0} | select Name, PrimarySmtpAddress
如何在 Exchange Server 中查找不活动的通讯组
Get-MessageTrackingLog cmdlet 在 Exchange Server 中用于分析传输日志。例如,您可以使用以下命令计算过去 90 天内发送到通讯组的电子邮件数量:
Get-MessageTrackingLog -Start (Get-Date).AddDays(-90) -ResultSize unlimited -Recipients "[email protected]"| measure-object
您可以使用以下命令将 Exchange Server 中电子邮件跟踪日志的保留期增加到 180 天:
Set-TransportService MUN-Ex01 -MessageTrackingLogMaxAge 180.00:00:00
要查找未使用的通讯组,您可以使用以下 PowerShell 脚本:
- 获取域中所有通讯组的列表并将其导出到 CSV 文件:
Get-DistributionGroup | Select-Object PrimarySMTPAddress | Sort-Object PrimarySMTPAddress | Export-CSV all-exchange-dls.csv –notype - 列出过去 30 天内收到电子邮件的 DL:
Get-MessageTrackingLog -Start (Get-Date).AddDays(-30) -EventId Expand -ResultSize Unlimited |Sort-Object RelatedRecipientAddress | Group-Object RelatedRecipientAddress |Sort-Object Name | Select-Object @{label= "PrimarySmtpAddress";expression={$_.Name}}, Count | Export-CSV exchange-active-dls.csv –notype如果您的 Exchange 组织有多于一台具有传输角色的服务器(您可以使用以下命令获取它们的列表)Get-TransportService),您需要搜索它们中的每一个:Get-MessageTrackingLog –Server Exh01 …。 - 然后比较两个列表并找到不活跃的组:
$alldl = Import-CSV -Path all-exchange-dls.csv
$activedl = Import-CSV -Path exchange-active-dls.csv
Compare-Object $alldl $activedl -Property PrimarySmtpAddress -SyncWindow 500 |Sort-Object PrimarySmtpAddress | Select-Object -Property PrimarySmtpAddress |Export-Csv inactive-dls.csv –NoType - 您可以在全局地址列表中隐藏未使用的通讯组:
$currentdate = Get-Date
$notes = "Inactive: hidden in the address list at $currentdate"
$inactiveDL = Import-CSV -Path inactive-dls.| foreach-object
{
Set-Group -identity $_.PrimarySmtpAddress -notes $notes
Set-DistributionGroup -identity $_.PrimarySmtpAddress -HiddenFromAddressListsEnabled $true
}
在 Microsoft 365 中,您可以使用 Exchange 管理中心 (邮件流程->消息追踪)或使用Start-HistoricalSearch或者Get-MessageTracePowerShell cmdlet。最后一个命令有一个很大的限制 - 它只允许您搜索过去 10 天内发送的电子邮件。这不适合我们的任务。
安装 Exchange Online PowerShell (EXO) 模块并连接到您的租户:
Connect-ExchangeOnline
以下命令显示发送到通讯组列表的 SMTP 地址的电子邮件数量:
Start-HistoricalSearch -ReportTitle "Global Admin DL" -StartDate 04/20/2023 -EndDate 06/20/2023 -ReportType MessageTrace -RecipientAddress global_admins@ woshub.com -NotifyAddress [email protected]
单个租户每 24 小时最多可以使用 250 条历史搜索。
要开始搜索非活动 DL,您可以使用以下脚本:
foreach ($group in Get-DistributionGroup)
{
Start-HistoricalSearch -ReportTitle $group.PrimarySmtpAddress -StartDate 04/20/2023 -EndDate 06/21/2023 -ReportType MessageTrace -RecipientAddress $group.PrimarySmtpAddress -NotifyAddress [email protected]
}
搜索完成后,您可以查看已发送到 DL 的电子邮件数量:
Get-HistoricalSearch "Global Admin DL"
如果消息列表为空(Rows = 0),这意味着该通讯组在过去 90 天内未在 Exchange Online 中使用。这样的分发列表可以被认为是不活动的。


您可以在 Exchange Online 中使用 Microsoft 365 组而不是通讯组列表。
