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

PowerShell中AD通讯组自定义属性设置问题求助

问题解决:通讯组属性(targetAddress、备用SMTP、Company)设置失败

原脚本核心问题

  • Alias参数不合法:Exchange通讯组的Alias不能包含@符号,原脚本里-Alias ($email + "@mydomain")违反规则,且会导致后续AD组身份匹配异常。
  • 备用SMTP地址被覆盖:直接使用-EmailAddresses会替换掉已创建的主SMTP地址,需用增量添加语法保留原有地址。
  • targetAddress属性操作错误:该属性属于Exchange管理范畴,不能用Set-ADGroup修改,必须通过Exchange cmdlet处理。
  • Set-ADGroup身份定位失败:用邮件地址作为-Identity无法精准匹配AD对象,需改用AD组的SAMAccountName或DN。

修正后的完整脚本

$emailPrefix = Read-Host -Prompt 'Enter the email prefix (without @mydomain)'
$dgName = Read-Host -Prompt 'Name of the DG'
$domain = "mydomain"
$companyName = "mycompany"
$targetAddr = "$emailPrefix`_$domain@cooperaction.mail.onmicrosoft.com"
$secondarySmtp = "$emailPrefix`_$domain.fr@cooperaction.mail.onmicrosoft.com"
$ouLDAPPath = "OU=YourTargetOU,DC=yourdomain,DC=com" # 替换为你的OU完整LDAP路径

# 创建通讯组,修正Alias参数并返回组对象
$newDG = New-DistributionGroup -Name $dgName `
    -Alias $emailPrefix `
    -DisplayName $dgName `
    -PrimarySmtpAddress "$emailPrefix@$domain" `
    -OrganizationalUnit $ouLDAPPath `
    -PassThru

# 设置Exchange属性:添加备用SMTP地址、配置targetAddress
Set-DistributionGroup -Identity $newDG.Identity `
    -EmailAddresses @{add="smtp:$secondarySmtp"} `
    -TargetAddress $targetAddr

# 设置AD属性:Company,用SAMAccountName精准定位组
Set-ADGroup -Identity $newDG.SamAccountName `
    -Replace @{Company= $companyName}

关键说明

  1. Alias规范:将Alias改为纯邮件前缀(不含@),既符合Exchange要求,也能作为AD组的SAMAccountName,解决后续身份匹配问题。
  2. 备用SMTP增量添加:使用@{add="smtp:xxx"}语法,在保留主SMTP地址的前提下添加备用地址,小写smtp:表示备用地址类型。
  3. targetAddress正确设置:直接通过Set-DistributionGroup的-TargetAddress参数配置,这是该属性的官方管理方式。
  4. AD组身份定位:利用New-DistributionGroup的-PassThru参数获取创建后的组对象,直接提取SamAccountName作为Set-ADGroup的标识,确保精准匹配。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 01:54:23