简体   繁体   English

操纵多属性AD属性(ProxyAddresses)

[英]Manipulating multiproperty AD attributes (ProxyAddresses)

I have a list of users with several values in their ProxyAddresses attribute eg 我有一个用户列表,在其ProxyAddresses属性中具有多个值,例如

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

and many other unknown ones. 和许多其他未知的人。

What I want to do is: 我想做的是:

  1. Convert all existing addresses that begin with smtp/SMTP to lowercase 将所有以smtp / SMTP开头的现有地址转换为小写
  2. Add/replace the one that conforms to the SMTP:firstname.surname@Domain2.com standard (to make it the primary) 添加/替换符合SMTP:firstname.surname@Domain2.com标准的文件(使其成为主要文件)

I've not got very far, running this just strips all proxyaddresses out and adds the one specified: 我还不太远,运行此命令只会剥离所有proxyaddress并添加指定的一个:

$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"}
} 

How do I enumerate through each value in a multivalue attribute? 如何枚举多值属性中的每个值?

The below code should do what you need. 下面的代码应满足您的需求。 It updates the ProxyAddresses multivalue property so that all 'smtp/SMTP' addresses will become lowercase and the new Primary emailaddress is computed and inserted in the list. 它会更新ProxyAddresses多值属性,以便所有“ smtp / SMTP”地址都变为小写,并计算出新的主电子邮件地址并将其插入列表中。

I have added a small helper function to replace diacritic characters that may appear in the users first or last name because especially Outlook 365 does not handle these characters very well. 我添加了一个小的帮助程序功能来替换可能在用户的名字或姓氏中出现的变音符号,因为特别是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:', '')
} 

Untested, because I don't have an AD at my disposal here, but I'd expect something like this to work, since multivalued attributes should be returned as collections. 未经测试,因为这里没有我可以使用的AD,但是我希望类似的东西可以工作,因为多值属性应该作为集合返回。

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

To assign the correct new primary address to each user you could map the addresses to usernames in a hashtable and then do a lookup rather than assign the new primary address as a static value: 要将正确的新主地址分配给每个用户,您可以将地址映射到哈希表中的用户名,然后进行查找,而不是将新主地址分配为静态值:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM