更新主要电子邮件地址

时间:2018-08-20 03:25:55

标签: powershell active-directory

我正在尝试更新AD中所有通讯组的主要电子邮件地址。

例如当前,所有组的代理地址字段都设置为SMTP:abc@contoso.com; smtp:abc@contoso2.com =>所以contoso是主要的,contoso2是次要的。 (注意:对于某些组,可以将多个电子邮件地址设置为辅助电子邮件地址,例如SMTP:abc@contoso.com; smtp:abc@contoso2.com; smtp:abc@contoso3.com; smtp:abc@contoso4.com)

我想将其更改为smtp:abc@contoso.com; SMTP:abc@contoso2.com =>将contoso2作为主要对象,将contoso作为辅助对象。 (将contoso3,contoso4等保留为辅助)

有人可以建议我如何通过PowerShell做到这一点吗?

3 个答案:

答案 0 :(得分:1)

看起来我可以使用以下脚本。尽管效果不佳,但我利用了我们无法在proxyAddresses字段中添加重复项的事实

Get-ADGroup -Filter 'groupcategory -eq "distribution"' -SearchBase "OU=Test Groups,DC=test,DC=local" -Properties * |
    select Name,mail,@{Name=’proxyAddresses’;Expression={$_.proxyAddresses -join ";"}} |
    Export-Csv "C:\temp\Distribution-Group-Members.csv" -NoTypeInformation

#Create new CSV file ,  update email column with beteast, add new column as "proxy" and add proxymailaddresses 
Import-Csv -Path 'C:\temp\Distribution-Group-Members.csv' |
    Select-Object -Property Name, @{Label='proxyAddresses'; Expression={"SMTP:$($_.Mail);$($_.proxyAddresses -replace 'SMTP', 'smtp')"}} |
    Export-Csv 'C:\temp\Distribution-Group-Members-updated.csv' -NoTypeInformation

#Update all DLs with correct email address and proxy email addresses 
Import-Csv "c:\temp\Distribution-Group-Members-Updated.csv" | Foreach {
    Get-ADGroup $_.Name |
        Set-ADGroup -add @{proxyaddresses=($_.proxyAddresses -split ";")}
}

答案 1 :(得分:0)

Import-Module ActiveDirectory 
Get-ADUser -Filter * -SearchBase 'DC=test,DC=net' -prop mail |
ForEach-Object { 
    Set-ADUser -Identity $_  -EmailAddress ($_.mail -replace "@abc.com","@123.com")
}

答案 2 :(得分:0)

我会执行以下操作来更改每个通讯组上的主要电子邮件地址:

# this is the email domain part that should become the primary address for every distribution group
$newPrimaryEmailDomain = 'contoso2.com'

Get-ADGroup -Filter "groupcategory -eq 'distribution'" -Properties * |
    Select-Object Name, Mail, proxyAddresses, DistinguishedName | ForEach-Object {
        $group = $_
        # get the current primary email address and remove the 'SMTP:' prefix
        $oldPrimaryAddress = ($group.ProxyAddresses | Where-Object { $_ -cmatch '^SMTP:.*' }) -replace '^SMTP:', ''
        # split the current primary address
        $emailName, $emailDomain = $oldPrimaryAddress -split '@'
        if ($newPrimaryEmailDomain -eq $emailDomain) {
            Write-Host "Group '$($group.Name)' already has the correct primary email address ($oldPrimaryAddress)"
            continue
        }

        # create the new primary email address
        $newPrimaryAddress = 'SMTP:{0}@{1}' -f $emailName, $newPrimaryEmailDomain
        # add the lowercase 'smtp:' prefix to the old address to make it secondary
        $oldPrimaryAddress = 'smtp:' + $oldPrimaryAddress

        # read all other existing addresses, excluding the old primary address and the new one should it be in the list as secondary address
        $otherAddresses = @($group.ProxyAddresses | Where-Object { ($_ -cnotmatch '^SMTP:.*') -and ($_ -ne $newPrimaryAddress)})
        # add the old primary address to the 'other' (secondary) addresses and remove any duplicates
        $otherAddresses = ($otherAddresses + $oldPrimaryAddress) | Sort-Object -Unique
        # recreate the complete proxy addresses array with the new Primary address on top
        $newProxyAddresses = @($newPrimaryAddress) + $otherAddresses

        # finally replace the group proxy addresses.:
        Set-ADGroup -Identity $group.DistinguishedName -Clear ProxyAddresses
        Set-ADGroup -Identity $group.DistinguishedName -Add @{'proxyAddresses'=$newProxyAddresses }
        Write-Host "Group '$($group.Name)' new primary email address: $($newPrimaryAddress -replace '^SMTP:', '')" -ForegroundColor Green
}