You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell脚本导入Outlook联系人时无法正确设置邮箱地址

问题:PowerShell导入AD用户到Outlook联系人时邮箱地址无法正确设置

使用PowerShell脚本将Active Directory用户导入Outlook联系人时,其他属性均可正常导入,但邮箱地址始终无法正确设置,表现为空或格式错误。已确认CSV文件中的邮箱字段内容正确,尝试过SMTP:xxx@domain.com、EX格式等多种写法,均未解决问题。

已执行步骤

  • 将AD用户导出至CSV文件,包含邮箱地址及其他属性
  • 通过PowerShell脚本遍历CSV数据,执行联系人的添加或更新操作
  • 在操作过程中,通过$user.'E-mail Address'为联系人的Email1Address属性赋值,但邮箱字段仍异常

脚本代码

foreach ($user in $csvData) {
    Write-Output "Processing user: $($user.'E-mail Address')"

    $existingContact = $ContactsFolder.Items | Where-Object { $_.Email1Address -eq $user.'E-mail Address' }

    if ($existingContact) {
        Write-Output "Updating existing contact: $($existingContact.Email1Address)"
        $existingContact.FirstName = $user.'First Name'
        $existingContact.MiddleName = $user.'Middle Name'
        $existingContact.LastName = $user.'Last Name'
        $existingContact.Email1Address = $user.'E-mail Address'
        $existingContact.HomeTelephoneNumber = $user.'Home Phone'
        $existingContact.BusinessTelephoneNumber = $user.'Business Phone'
        $existingContact.MobileTelephoneNumber = $user.'Mobile Phone'
        $existingContact.BusinessFaxNumber = $user.'Business Fax'
        $existingContact.JobTitle = $user.'Job Title'
        $existingContact.Department = $user.'Department'
        $existingContact.CompanyName = $user.'Company'
        $existingContact.OfficeLocation = $user.'Office Location'
        $existingContact.ManagerName = $user.'Manager''s Name'
        $existingContact.BusinessAddressStreet = $user.'Business Street'
        $existingContact.BusinessAddressCity = $user.'Business City'
        $existingContact.BusinessAddressState = $user.'Business State'
        $existingContact.BusinessAddressPostalCode = $user.'Business Postal Code'
        $existingContact.BusinessAddressCountry = $user.'Business Country/Region'

        $existingContact.Save()
    } else {
        Write-Output "Creating new contact for: $($user.'E-mail Address')"
        $NewContact = $ContactsFolder.Items.Add("IPM.Contact")
        $NewContact.FirstName = $user.'First Name'
        $NewContact.MiddleName = $user.'Middle Name'
        $NewContact.LastName = $user.'Last Name'
        $NewContact.Email1Address = $user.'E-mail Address'
        $NewContact.HomeTelephoneNumber = $user.'Home Phone'
        $NewContact.BusinessTelephoneNumber = $user.'Business Phone'
        $NewContact.MobileTelephoneNumber = $user.'Mobile Phone'
        $NewContact.BusinessFaxNumber = $user.'Business Fax'
        $NewContact.JobTitle = $user.'Job Title'
        $NewContact.Department = $user.'Department'
        $NewContact.CompanyName = $user.'Company'
        $NewContact.OfficeLocation = $user.'Office Location'
        $NewContact.ManagerName = $user.'Manager''s Name'
        $NewContact.BusinessAddressStreet = $user.'Business Street'
        $NewContact.BusinessAddressCity = $user.'Business City'
        $NewContact.BusinessAddressState = $user.'Business State'
        $NewContact.BusinessAddressPostalCode = $user.'Business Postal Code'
        $NewContact.BusinessAddressCountry = $user.'Business Country/Region'

        $NewContact.Save()
    }
}

解决方案

Outlook对象模型中直接赋值Email1Address容易出现格式解析问题,可通过以下两种方法解决:

方法1:通过收件人对象设置邮箱

创建或更新联系人时,不要直接赋值Email1Address,而是通过添加收件人并解析的方式关联邮箱:

foreach ($user in $csvData) {
    Write-Output "Processing user: $($user.'E-mail Address')"
    $email = $user.'E-mail Address' -replace '^SMTP:', '' # 移除可能的前缀

    $existingContact = $ContactsFolder.Items | Where-Object { 
        $_.Email1Address -eq $email -or $_.Email1DisplayName -eq $email
    }

    if ($existingContact) {
        Write-Output "Updating existing contact: $($existingContact.FullName)"
        # 清空原有收件人
        $existingContact.Recipients.RemoveAll()
        # 添加并解析收件人
        $recipient = $existingContact.Recipients.Add($email)
        $recipient.Type = 1 # 1代表SMTP类型
        if ($recipient.Resolve()) {
            $existingContact.Email1EntryID = $recipient.EntryID
        }
        # 更新其他属性(保留原有属性赋值代码)
        $existingContact.Save()
    } else {
        Write-Output "Creating new contact for: $email"
        $NewContact = $ContactsFolder.Items.Add("IPM.Contact")
        # 添加并解析收件人
        $recipient = $NewContact.Recipients.Add($email)
        $recipient.Type = 1
        if ($recipient.Resolve()) {
            $NewContact.Email1EntryID = $recipient.EntryID
        }
        # 设置其他属性(保留原有属性赋值代码)
        $NewContact.Save()
    }
}

方法2:标准化邮箱格式后直接赋值

如果要继续使用直接赋值的方式,需确保邮箱为纯SMTP格式(如user@domain.com),移除SMTP:前缀,并在赋值前验证格式:

$email = $user.'E-mail Address' -replace '^SMTP:', ''
# 验证邮箱格式
if ($email -match '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$') {
    if ($existingContact) {
        $existingContact.Email1Address = $email
    } else {
        $NewContact.Email1Address = $email
    }
}

注意事项

  • 运行脚本前需确保Outlook已启动,且当前用户拥有联系人文件夹的访问权限
  • 更新现有联系人时,建议先清空原邮箱地址再重新赋值,避免缓存干扰

内容的提问来源于stack exchange,提问作者Din Sadovic

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 20:05:05