[英]Powershell: Replacing spaces with periods in this active directory script
Sorry if this is quite simple, but I am new to powershell. 抱歉,这很简单,但是我是Powershell的新手。
I am currently using the following script to add proxyAddress values to users in Active Directory: 我目前正在使用以下脚本向Active Directory中的用户添加proxyAddress值:
Get-ADUser -Filter * -SearchBase 'OU=myou,DC=mydc' -Properties proxyaddresses |
Foreach {Set-ADUser -identity $_ -Add `
@{'ProxyAddresses'=@(("{0}{1}@{2}"-f 'smtp:', $_.name, 'mydomain.com'),("{0}{1}. {2}@{3}" -f 'SMTP:', $_.givenName, $_.Surname, 'mydomain.com'))} }
However, for the next few OU's the givenName and surname values are blank, the name is stored in displayName in the following format "Firstname Surname". 但是,对于接下来的几个OU,givenName和Surname值为空白,该名称以以下格式“ Firstname Surname”存储在displayName中。
How can I modify the script so instead of taking givenName period Surname, it will take displayName and replace the whitespace with a period? 我该如何修改脚本,而不是使用给定的名称句点姓氏,而是使用displayName并将空格替换为句点?
EG A user with displayName "Joe Bloggs" would be given the value SMTP:Joe.Bloggs@mydomain.com 例如,显示名称为“ Joe Bloggs”的用户将被赋予值SMTP:Joe.Bloggs@mydomain.com
Not tested, but something like this maybe: 未经测试,但是类似这样的东西:
Get-ADUser -Filter * -SearchBase 'OU=myou,DC=mydc' -Properties proxyaddresses,displayname |
Foreach {
$ProxyAddresses = @("{0}{1}@{2}"-f 'smtp:', $_.name, 'mydomain.com')
if ($_.surname)
{ $ProxyAddresses += "{0}{1}. {2}@{3}" -f 'SMTP:', $_.givenName, $_.Surname, 'mydomain.com'}
else
{
$UserPart = $_.DisplayName.trim().replace(' ','.')
$ProxyAddresses += "{0}{1}@{2}"-f 'smtp:', $UserPart, 'mydomain.com'
}
}
Set-ADUser -identity $_ -Add @{'ProxyAddresses'= $ProxyAddresses }
Try something like this. 尝试这样的事情。 A simple replace operation on the value of displayname, and a few adjustments.
对displayname的值进行简单的替换操作,并进行一些调整。
#Include displayname property
Get-ADUser -Filter * -SearchBase 'OU=myou,DC=mydc' -Properties displayname, proxyaddresses |
Foreach {
#Used ($_.displayname.trim() -replace ' ', '.') and modified string format.
Set-ADUser -identity $_ -Add @{
'ProxyAddresses'=@(("{0}{1}@{2}"-f 'smtp:', $_.name, 'mydomain.com'),("{0}{1}. {2}@{3}" -f 'SMTP:', ($_.displayName.trim() -replace ' ', '.'), 'mydomain.com'))
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.