我正在 Exchange 管理 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."
}
}
答案1
您是否已确保列表中的所有地址都是用户的默认主地址,因为查询时不会找到分配给该帐户的任何其他别名$_.PrimarySMTPAddress
一种检查方法是使用Get-Recipient
since 查询“丢失”的地址,这样无论它在哪里都可以找到地址,就像这样
Write-Host($user)
if ($user) {
Add-DistributionGroupMember -Identity $groupName -Member $user.Identity
Write-Host "Added $($user.PrimarySMTPAddress ) to $groupName"
} else {
$recipient = Get-Recipient $email -ErrorAction SilentlyContinue
if ($recipient)
{
Write-Host "$email found in $($recipient.Name) - $($recipient.RecipientType)"
}
else
{
Write-Host "User with email $email not found."
}
}
显示RecipientType
其被分配到的对象类型(如果它不是用户邮箱)。