我正在交换管理 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."
}
}