[英]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.