[英]Updating user ProxyAddresses
我嘗試更新 AD 用戶帳戶 ProxyAddresses 屬性。 我已經閱讀了很多關於此的主題並應用了一種建議的方法( this one ),但它對我不起作用。 為什么? 我使用了以下代碼(它是更新更多用戶數據的腳本的一部分):
$ADUser = SearchADUser -Kogo "sAMAccountName -eq '$($WzorUser.sAMAccountName)'"
...
1.$ProxyOK = $false
2.$Proxies = $ADUser.ProxyAddresses
3.$Proxies | ForEach-Object {
4. $_ = $_ -replace 'SMTP', 'smtp'
5. if ($_ -match $NoweMail) {
6. $_ = $_ -replace 'smtp', 'SMTP'
7. $ProxyOK = $true
8. }
9.}
10.if (!($ProxyOK)) { $Proxies += ("SMTP:$($NoweMail)") }
...
if (!([string]::IsNullOrEmpty($Proxies))) {
$AttrToReplace.Add("ProxyAddresses", $Proxies)
Set-ADUser -Identity $ADUser.sAMAccountName -Server $ADDC @Attr #-PassThru -WhatIf
在代理上循環時,它的元素被正確處理:如果新郵件已經存在,則所有字母都是小寫的,而大寫的。 但是代理沒有改變。 每個元素是否需要以某種方式保存或在對象中替換?
更新 2020-04-02
因為支持者的努力(@Theo 再次感謝您的幫助)專注於替換方法,所以我嘗試更詳細地解釋我的問題。
目標用戶 proxyAddresses 值(很少有女性回到 AD 中記錄的娘家姓):
SMTP :anna.nowak22@lp.pl
SMTP:ab@moc.com
初始值(第 2 行):
[DBG]: PS X:\>> $Proxies
smtp:anna.nowak22@lp.pl
SMTP:a.b@moc.com
在每個代理元素上循環時(第 9 行):
[DBG]: PS X:\>> $_
SMTP:anna.nowak22@lp.pl
[DBG]: PS X:\>> $Proxies
smtp:anna.nowak22@lp.pl
SMTP:a.b@moc.com
[DBG]: PS X:\>> $_
smtp:a.b@moc.com
[DBG]: PS X:\>> $Proxies
smtp:anna.nowak22@lp.pl
SMTP:a.b@moc.com
可以看出 $Proxies 不反映變化。
如果只有一個 ProxyAddresses 值與新郵件不相等,則將新郵件作為 SMTP 添加到現有郵件中,該郵件也保留為 SMTP(兩個主要 ProxyAddresses)。
我試圖創建一個新變量並分別為其分配每個值,但我不知道如何處理它。
$newProxies = $null
$Proxies | ForEach-Object {
$_ = $_ -creplace 'SMTP', 'smtp'
if ($_ -match $NoweMail) {
$_ = $_ -creplace 'smtp', 'SMTP'
$ProxyOK = $true
}
$newProxies.add($_)
}
以上產生錯誤
不能在空值表達式上調用方法
$newProxies += $_
創建一個字符串SMTP:anna.nowak22@lp.pl smtp:ab@moc.com作為單個 ProxyAddress 添加。
正如我指出的 $Proxies 是一個特殊的 AD 對象,我不知道如何創建這種類型的對象以及如何向其中添加新元素。
正如評論的那樣,這里作為答案更具可讀性。
在您的代碼中,您將修改后的$Proxies
數組添加到名為$AttrToReplace
的哈希表(我認為)中。 但是,在最后的Set-ADUser
命令中,您沒有使用它,而是使用變量@Attr
splat 。
要更新多值屬性 ProxyAddresses,請更改問題中三個點之后的代碼:
if (!([string]::IsNullOrEmpty($Proxies))) {
$AttrToReplace.Add("ProxyAddresses", $Proxies)
Set-ADUser -Identity $ADUser.sAMAccountName -Server $ADDC @Attr #-PassThru -WhatIf
進入:
# no need to test if $Proxies is an empty array, because
# it will at the very least have "SMTP:$($NoweMail)"
Set-ADUser -Identity $ADUser.SamAccountName -Clear ProxyAddresses
Set-ADUser -Identity $ADUser.SamAccountName -Add @{proxyAddresses = $Proxies | ForEach-Object { "$_" }}
# or do this on one line:
# Set-ADUser -Identity $ADUser.SamAccountName -Replace @{proxyAddresses = $Proxies | ForEach-Object { "$_" }}
根據您的評論,您的代碼確實為重復的代理地址留下了空間。
你可以得到這個列表,用smtp:
替換所有現有的SMTP:
smtp:
並像這樣添加新的主電子郵件地址:
# get the current ProxyAddress values for this user, change all values # that begin with uppercase 'SMTP:' to lowercase 'smtp:'. # skip any proxy that is equal to "smtp:$NoweMail", add the $NoweMail # preceeded by 'SMTP:' to the list and sort or select unique $Proxies = @($ADUser.ProxyAddresses -replace '^SMTP:', 'smtp:' | Where-Object { $_ -ne "smtp:$NoweMail" }) + "SMTP:$NoweMail" | Sort-Object -Unique # set the new proxy addresses in the user attribute # no need to test if $Proxies is an empty array, because # it will at the very least have "SMTP:$($NoweMail)" Set-ADUser -Identity $ADUser.SamAccountName -Clear ProxyAddresses Set-ADUser -Identity $ADUser.SamAccountName -Add @{proxyAddresses = [string[]]$Proxies} # or do this on one line: # Set-ADUser -Identity $ADUser.SamAccountName -Replace @{proxyAddresses = [string[]]$Proxies}
請注意,在要添加或替換的哈希內部,使用的是 LDAP 名稱,因此使用小寫p
proxyAddresses
@西奧。 您的解決方案工作正常。 謝謝你的幫助。 我保持我的方法(它適合我並且有效)並稍微修改了代碼。
$ProxyOK = $false
$Proxies = $null
$Proxies = @()
$ADUser.ProxyAddresses | ForEach-Object {
$_ = $_ -creplace 'SMTP', 'smtp'
if ($_ -match $NoweMail) {
$_ = $_ -creplace 'smtp', 'SMTP'
$ProxyOK = $true
}
$Proxies += $_
}
if (!($ProxyOK)) { $Proxies += ("SMTP:$($NoweMail)") }
...
if ($Proxies) {
$AttrToReplace.Add("ProxyAddresses", [string[]]$Proxies)
}
if ($AttrToReplace.Count -gt 0) {
$Attr.Add("Replace", $AttrToReplace)
}
Set-ADUser -Identity $ADUser.sAMAccountName -Server $ADDC @Attr #-PassThru -WhatIf
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.