操纵多属性AD属性(ProxyAddresses)

时间:2018-10-10 15:35:17

标签: powershell active-directory

我有一个用户列表,这些用户在其ProxyAddresses属性中具有多个值,例如

SMTP:JohnSmith@domain1.com
smtp:jsmith@domain2.com
smtp:ukCC10s@domain2.com
smtp:smith.john@domain3.com

和许多其他未知的人。

我想做的是:

  1. 将所有以smtp / SMTP开头的现有地址转换为小写
  2. 添加/替换符合 SMTP:firstname.surname@Domain2.com 标准(使其成为主要标准)的邮件

我还不算太远,运行它只会剥离所有代理地址并添加指定的地址:

$userou = "OU=test2,OU=Test,OU=Users,DC=Corp,DC=Local"
$users = Get-ADUser -Filter * -SearchBase $userou -Properties SamAccountName, ProxyAddresses

foreach ($user in $users) {
    Get-ADUser $user | Set-ADUser -Replace @{'ProxyAddresses'="SMTP:blah@blah.com"}
} 

如何枚举多值属性中的每个值?

2 个答案:

答案 0 :(得分:2)

以下代码应满足您的需求。 它会更新ProxyAddresses多值属性,以便所有“ smtp / SMTP”地址都变为小写,并计算出新的主电子邮件地址并将其插入列表中。

我添加了一个小的帮助程序功能来替换可能出现在用户名字或姓氏中的变音符号,因为特别是Outlook 365不能很好地处理这些字符。

$userou = "OU=test2,OU=Test,OU=Users,DC=Corp,DC=Local"
$users  = Get-ADUser -Filter * -SearchBase $userou -Properties SamAccountName, ProxyAddresses, EmailAddress


function Replace-Diacritics {
    # helper function to replace characters in email addresses that especially Outlook365 does not like..
    Param(
        [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
        [string] $EmailAdress
    )
    $chars = @()
    $normalized = $EmailAdress.Normalize( [Text.NormalizationForm]::FormD )
    $normalized.ToCharArray() | ForEach-Object { 
        if( [Globalization.CharUnicodeInfo]::GetUnicodeCategory($_) -ne [Globalization.UnicodeCategory]::NonSpacingMark) {
            $chars += $_
        }
    }
    $chars -join ''
}

foreach ($user in $users) {
    # create the new primary emailaddress
    # remove invalid characters and replace diacritics ("Frédérique.Étrangér@Domain2.com" --> "frederique.etranger@domain2.com")
    $newPrimary = ($("{0}.{1}@Domain2.com" -f $user.GivenName, $user.Surname) -replace '[\s()<>,;:''"{}/[\]\\]+', '').ToLower()
    $newPrimary = "SMTP:" + (Replace-Diacritics ($newPrimary -replace '\.+', '.'))

    # get all email addresses and convert them to lowercase. At the same time dedupe this array.
    # this will also replace 'SMTP:' of the former Primary email address to become an alias ('smtp:')
    $emailAliases = @($user.ProxyAddresses | Where-Object { $_ -match '^smtp:.*' -and $_ -ne $newPrimary } | 
                                             ForEach-Object { $_.ToLower() } |
                                             Sort-Object -Unique)
    # read all other existing stuff
    $otherAddresses = @($user.ProxyAddresses | Where-Object { $_ -notmatch '^smtp:.*' })

    # now merge all addresses into one array, the Primary email address on top for easier reading in ADUC
    $newProxies = (@($newPrimary) + $emailAliases) + $otherAddresses

    # finally replace the users ProxyAddresses property. I like:
    $user | Set-ADUser -Clear ProxyAddresses
    $user | Set-ADUser -Add @{'proxyAddresses'=$newProxies }
    # but you could also do
    # $user | Set-ADUser -Replace @{'proxyAddresses' = $newProxies}

    # finally, put the new primary email address in the users 'mail' property
    $user | Set-ADUser -EmailAddress $($newPrimary -replace 'SMTP:', '')
} 

答案 1 :(得分:0)

未经测试,因为我在这里没有可用的AD,但是我希望类似的东西可以工作,因为多值属性应该作为集合返回。

$addr = $user.ProxyAddresses -creplace '^SMTP:', 'smtp:'
$addr += 'SMTP:blah@blah.com'
$user | Set-ADUser -Replace @{ 'ProxyAddresses' = $addr }

要将正确的新主地址分配给每个用户,您可以将地址映射到哈希表中的用户名,然后进行查找,而不是将新主地址分配为静态值:

$addr += $newPrimaryAddress[$user.SamAccountName]