Powershell ForEach替换(大容量更改AD中的主SMTP)
我需要从某个OU的用户批量切换AD中的主SMTP地址 挑战Powershell ForEach替换(大容量更改AD中的主SMTP),powershell,active-directory,powershell-2.0,powershell-3.0,powershell-4.0,Powershell,Active Directory,Powershell 2.0,Powershell 3.0,Powershell 4.0,我需要从某个OU的用户批量切换AD中的主SMTP地址 挑战 User1 smtp:first.last@domain1.com smtp:flast@domain1.com SMTP:first.last@domain2.net smtp:flast@domain2.net 我需要做第一件事。last@domain1主SMTP 到目前为止,我已经做到了这一点 $proxies = $null Get-ADUser -Filter * -SearchBase "OU
User1
smtp:first.last@domain1.com
smtp:flast@domain1.com
SMTP:first.last@domain2.net
smtp:flast@domain2.net
我需要做第一件事。last@domain1主SMTP
到目前为止,我已经做到了这一点
$proxies = $null
Get-ADUser -Filter * -SearchBase "OU=users_test,OU=Test,DC=test,DC=local" -Properties name,mail,ProxyAddresses |
Foreach {
$proxies = $_.ProxyAddresses |
ForEach-Object{
$a = $_ -replace 'SMTP','smtp'
if($a -match 'domain1.com'){
$a -replace 'smtp','SMTP'
Write-Host $a
}else{
$a
}
}
$_.ProxyAddresses = $proxies
#Set-ADUser -instance $_
Write-host $proxies
}
问题是:
当我运行上面的脚本时,它显然会通过在所有与domain1.com匹配的文件上用smtp替换smtp,使domain1.com的两个别名成为主要别名
问题:我如何使它只替换一个
我希望我能很好地解释我自己。提前感谢您提供的任何帮助,因为没有必要选择一个特定的domain1.com地址,请尝试此项 我添加了一个flag变量,以便每个用户只设置一次主地址。 此外,我切换到了-like操作符,因为-match操作符不是必需的,如果没有正确使用,只会产生更多的开销 我在替换部件中添加了“字符串开头”正则表达式字符(-replace也使用正则表达式模式)
Get ADUser-Filter*-SearchBase'OU=users\u test,OU=test,DC=test,DC=local'-属性名称、邮件、代理地址|
ForEach对象{
#标记以避免在第一次匹配后进行进一步处理
$userDone=$false
$proxies=$\代理地址|
ForEach对象{
$proxyAddress=$\替换“^SMTP”、“SMTP”
if(!$userDone-和$proxyAddress-如'*@domain1.com'){
$proxyAddress-替换“^smtp”、“smtp”
$userDone=$true
}否则{
$proxyAddress
}
}
$\ ProxyAddresses=$代理
#设置ADUser-实例$_
写入主机$proxies
}
更新2021-01-13
根据您在下面评论中的要求,这里有一个更新
你能告诉我如何使用第一个选择的相同脚本吗。last@domain1.com. ForEach应更改为primary,即具有first.last的ForEach
现在正则表达式更有意义了;)
该代码未针对Active Directory进行测试,但应能正常工作
简而言之,正则表达式模式:
(?i) >case-insensitive match (=regex option)
^ >start of string
(?: >non-capturing group (capturing is not required in your case)
smtp: >starts with 'smtp:'
[^\.\@]+ >matches any char at least once excluding '.' and '@'
\. >matches '.' once
[^\.\@]+ >matches any char at least once excluding '.' and '@'
@domain1\.com >matches '@domain1.com'
)
$ >end of string
有关更多详细信息,请参阅:
我还添加了一个警告,当由于任何原因没有进行匹配时。然后,地址不会返回到源属性(地址保持原始)
#模式仅匹配格式为“*.@domain.com”->。@domain.com的地址
$newPrimaryAddressMatchPattern='(?i)^(?:smtp:[^\.\@]+\.[^\.\@]+@domain1\.com)$'
Get ADUser-Filter*-SearchBase'OU=users\u test,OU=test,DC=test,DC=local'-属性名称,邮件,代理地址|
ForEach对象{
#标记以避免在第一次匹配后进行进一步处理
$userDone=$false
$proxies=$\代理地址|
ForEach对象{
$proxyAddress=$\替换“^SMTP”、“SMTP”
如果(!$userDone-和$proxyAddress-匹配$newPrimaryAddressMatchPattern){
$proxyAddress-替换“^smtp”、“smtp”
$userDone=$true
}否则{
$proxyAddress
}
}
如果(!$userDone){
#如果没有地址与设置新主地址所需的模式匹配
写入警告“无法为$($).UserPrincipalName)|$($).CanonicalName)设置新的主地址!”
}否则{
$\ ProxyAddresses=$代理
}
#设置ADUser-实例$_
写入主机$proxies
}
哪个(domain1.com地址)应该是主地址?左半部(在@之前)与当前部分相同的那一个??注意:不要在regex匹配模式中使用domain1.com
。
将匹配任何内容(它是一个类似于*
的通配符)。使用domain1\.com$
@pwnosh-match不是regex(我想)。哪一个域名别名并不重要,只要它只替换一个域名别名即可。现在脚本正在用domain1.com替换所有别名。但理想情况下,这将是第一次。last@domain1.com@Heyvoon-匹配
是正则表达式。。。看一下帮助,非常感谢@pwnosh我已经更新了我的答案。检查一下它是否有效。如前所述,它未经测试,但应该有效;)再次感谢,我修改了脚本,将新的主SMTP地址设置为“mailaddress”属性。新代码可以在下面找到;