【问题标题】:Manipulating multiproperty AD attributes (ProxyAddresses)操作多属性 AD 属性 (ProxyAddresses)
【发布时间】:2018-10-10 15:35:17
【问题描述】:

我有一个用户列表,他们的 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"}
} 

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

【问题讨论】:

    标签: powershell active-directory


    【解决方案1】:

    下面的代码应该可以满足您的需求。 它更新 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:', '')
    } 
    

    【讨论】:

    • 谢谢,我试试看!
    • @Aziz 请查看编辑后的代码。之前的代码有一个错误,可能意味着丢失了以前的主要电子邮件地址。新代码将这个旧的主地址转换为别名。
    【解决方案2】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多