[英]Manipulating multiproperty AD attributes (ProxyAddresses)
我有一個用戶列表,在其ProxyAddresses
屬性中具有多個值,例如
SMTP:JohnSmith@domain1.com smtp:jsmith@domain2.com smtp:ukCC10s@domain2.com smtp:smith.john@domain3.com
和許多其他未知的人。
我想做的是:
我還不太遠,運行此命令只會剝離所有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"}
}
如何枚舉多值屬性中的每個值?
下面的代碼應滿足您的需求。 它會更新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:', '')
}
未經測試,因為這里沒有我可以使用的AD,但是我希望類似的東西可以工作,因為多值屬性應該作為集合返回。
$addr = $user.ProxyAddresses -creplace '^SMTP:', 'smtp:'
$addr += 'SMTP:blah@blah.com'
$user | Set-ADUser -Replace @{ 'ProxyAddresses' = $addr }
要將正確的新主地址分配給每個用戶,您可以將地址映射到哈希表中的用戶名,然后進行查找,而不是將新主地址分配為靜態值:
$addr += $newPrimaryAddress[$user.SamAccountName]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.