PowerShell 提取所有没有成员的空 DL

PowerShell to pull all empty DLs with no members

我希望有人能在这里帮助我。由于 Google 的帮助,我能够在线找到一个 PowerShell 脚本,它显示了我们环境中所有空的 DL,因为我们正在尝试进行清理。这是该脚本:

Get-DistributionGroup -ResultSize Unlimited |? {!(Get-DistributionGroupMember $_.PrimarySMTPAddress).Count} | select DisplayName,PrimarySMTPAddress | Export-Csv DLsToRemove3.csv

我在其中添加了一个 Export-Csv 以便将列表放入文件中。我开始查看列表并注意到列出的某些 DL 实际上有一个成员。在这一点上,我尝试 运行 针对我的 CSV 文件的另一个脚本,以获取包含其中一个成员和那个成员的任何 DL 的列表。这是该脚本:

Import-Csv "C:\Users7626\DLsToRemove3.csv" | foreach {$Group=$_.PrimarySmtpAddress; Get-DistributionGroupMember -Identity $Group | select @{Name="Group";Expression={$Group}}, DisplayName | Export-Csv Members.csv -NoType}

当我 运行 这样做时,我的 CSV 中根本没有填充任何信息。我正在寻求帮助,要么能够将第二步添加到第一步并将两个脚本合并为一个脚本,要么至少能够让第二个脚本工作以查看其中有一个成员的 DL。

谢谢!

试试这个。

Get-DistributionGroup -ResultSize Unlimited | ? { (Get-DistributionGroupMember $_.PrimarySMTPAddress | Measure-Object).Count -eq 0 } | select DisplayName,PrimarySMTPAddress | Export-Csv DLsToRemove3.csv

Measure-Object 在计算数组中的对象时更可靠。

我从来没有让空的 DL 失败

$emptyGroups = foreach ($grp in Get-DistributionGroup -ResultSize Unlimited) {
    if (@(Get-DistributionGroupMember –Identity $grp.DistinguishedName -ResultSize Unlimited).Count –eq 0 ) {
        [PsCustomObject]@{
            DisplayName        = $grp.DisplayName
            PrimarySMTPAddress = $grp.PrimarySMTPAddress
            DistinguishedName  = $grp.DistinguishedName
        }
    }
}
$emptyGroups | Export-Csv 'C:\Users7626\DLsToRemove4.csv' -NoTypeInformation

@() 强制将 Get-DistributionGroupMember 结果放入数组中以获得准确的 .Count 属性

属性 msExchGroupMemberCount 由 Exchange 维护,因此更快的方法是使用 get-adgroup.

过滤该属性
get-adgroup -Filter "msExchGroupMemberCount -eq 0" -Properties DisplayName,mail | select DisplayName,mail