我正在交换管理 shell 中运行下面的脚本。这样做的目的是,我想将 Excel 文件(文件有两列,姓名和电子邮件)中的电子邮件列表添加到组中。正如您所看到的,我已经导入了 Excel,添加了组并为 Excel 中的每个成员循环添加到组中。
问题是,我收到大量名称的“未找到电子邮件的用户”,即使它们确实存在。即使我可以验证该电子邮件确实存在,也可能大约 40% 已找到,60% 未找到。想知道是否有人可以看到我下面发布的脚本中的问题。
$csvPath = "C:\Users\Desktop\emailNames.csv"
# Replace with the name of the distribution group
$groupName = "emailOfficer"
# Import the CSV file
$members = Import-Csv -Path $csvPath
# Loop through the CSV and add members to the distribution group
foreach ($member in $members) {
$email = $member.email.Trim()
$user = Get-Mailbox | Where-Object { $_.PrimarySMTPAddress -ieq $email.Trim() }
# $user = Get-Mailbox | Where-Object { $_.PrimarySMTPAddress -ilike "*$email*" }
Write-Host($user)
if ($user) {
Add-DistributionGroupMember -Identity $groupName -Member $user.Identity
Write-Host "Added $($user.PrimarySMTPAddress ) to $groupName"
} else {
Write-Host "User with email $email not found."
}
}
您是否确保列表中的所有地址都是用户的默认主要地址,因为查询时不会找到分配给该帐户的任何其他别名
$_.PrimarySMTPAddress
一种检查方法是查询“丢失”的地址,
Get-Recipient
因为无论它在哪里都会找到一个地址,如下所示显示
RecipientType
分配给的对象类型(如果它不是用户邮箱)。