使用PowerShell迁移Exchange本地通讯组至365时遇问题求助
本地Exchange通讯组迁移至Office 365失败问题排查与修复
问题描述
尝试使用PowerShell脚本将本地Exchange通讯组迁移至Office 365时运行失败,错误提示:
无法将值“@{Add="SMTP:testgroup@xxx.com","smtp:testgroup@yyy.com"}”转换为类型“Microsoft.Exchange.Data.ProxyAddressCollection”。错误:“无法分析该字符串。”
原脚本内容
#Connect to Exchange Online #Connect-ExchangeOnline #Source - Provide the name of the on-prem distribution group you wish to migrate $DistGroup = "test group" #To avoid naming conflicts, we will append the word copy to the end of the migrate distribution group name $Append = "copy" #Name for the new cloud distribution group $CloudGroup = "$DistGroup $Append" #Import the csv file with all of the on-prem distribution groups #Target the on-prem distribution group you wish to copy $DistGroupCSV = Import-csv -path "\\vhall02\it\GADGroup\projects\distribution list\dlist.csv" | where-object {$_.\"display name\" -eq $DistGroup} $DistGroupname = $DistGroupCSV.'DISPLAY NAME' $c = Get-DistributionGroup -identity $DistGroupname $smtpaddresses = $c.EmailAddresses New-DistributionGroup -Name $CloudGroup -Type "distribution" $var = '@{Add=' foreach ($smtpaddress in $smtpaddresses){ $var = $var+ '"'+$smtpaddress+'",' } $var = $var.substring(0,$var.length-1) $var = $var+ '}' Set-DistributionGroup -Identity $CloudGroup -EmailAddresses $var Get-DistributionGroup $CloudGroup | format-table
问题原因
脚本中手动拼接-EmailAddresses参数的哈希表字符串是错误操作。PowerShell需要接收实际的哈希表对象,而非字符串形式的哈希表,因此无法解析拼接出来的字符串,导致类型转换失败。
修复后的脚本
# 连接Exchange Online(取消注释启用) # Connect-ExchangeOnline # 源本地通讯组名称 $DistGroup = "test group" # 避免命名冲突,添加后缀 $Append = "copy" # 云端新通讯组名称 $CloudGroup = "$DistGroup $Append" # 导入CSV并定位目标组 $DistGroupCSV = Import-Csv -Path "\\vhall02\it\GADGroup\projects\distribution list\dlist.csv" | Where-Object { $_.["display name"] -eq $DistGroup } $DistGroupname = $DistGroupCSV.'DISPLAY NAME' # 获取本地通讯组信息 $c = Get-DistributionGroup -Identity $DistGroupname $smtpAddresses = $c.EmailAddresses # 创建云端通讯组 New-DistributionGroup -Name $CloudGroup -Type "distribution" # 直接构建哈希表添加邮箱地址,无需手动拼接字符串 Set-DistributionGroup -Identity $CloudGroup -EmailAddresses @{ Add = $smtpAddresses } # 验证迁移结果 Get-DistributionGroup $CloudGroup | Format-Table
额外说明
- 运行前请取消注释
Connect-ExchangeOnline,确保已正确连接到Exchange Online环境 - 如果需要替换云端组的所有邮箱地址(而非追加),可将
@{ Add = $smtpAddresses }改为@{ Replace = $smtpAddresses }
内容的提问来源于stack exchange,提问作者Elroy1995
相关产品推荐
相关产品推荐

