简体   繁体   中英

PowerShell change proxy addresses in Active Directory with Set-ADUser

I have a small code where I don't know if it should work, because i'm at work at the moment and the proxy addresses are only created in production, and I'm afraid that the code wont work when I use it in production.

So the code logic is : -getting the proxy addresses -if the proxy address starts with "smtp:" , it will change to "smtp:d_"

For example, "smtp:test@gmail.com" would change to "smtp:d_test@gmail.com"

Here's my code :

#### proxy adresses
$Obj_CompteMPOP3 = Get-ADUser $login -Properties proxyaddresses -server $serverSource
$NewProxyAddresses = @() #new array of proxy addresses

$Proxies = $Obj_CompteMPOP3.proxyaddresses #get the proxy addresses of the LDAP object
foreach ($Proxy in $Proxies) {
    if ($Proxy.Substring(0, 5) -like 'smtp:*') #if the address starts with smtp:
    {
        $ProxyPrefixe = $Proxy.Substring(0, 5)
        $NewProxy = "$ProxyPrefixe$D_$($proxy.Substring(5))" #make the new proxy address with the format "smtp:" -> "smtp:d_"
        $MPOP3_NewProxyAddresses += $NewProxy #add the value to the new array of proxy addresses

        Set-ADUser $login -Replace @{ProxyAddresses = $MPOP3_NewProxyAddresses} -Server $serverSource -Credential $CredActivateSIRHMPOP3 #set the new addresses with the new array
    }

}

The line which i'm not sure at all is the set-aduser . Can it be used? Seems logical to me considering the get-aduser gives you back an array

Thanks in advance guys!

The set ad-user will work as a command but you need to add the value as an array.

From my experience, when we changed our mail server I had to export all the user addresses and add them to the proxy address attribute with the primary having capital SMTP.

What I did was:

Get-ADUser -Filter {(Enabled -eq $true) -and (sAMAccountType -ne 805306370) -and (cn -ne "Administrator")} -SearchBase "CN=,DC=,DC=,DC=" -Properties proxyAddresses |
Select Name, GivenName, Surname, Enabled, SamAccountName, Title,  `
@{L='ProxyAddress_1';E={$_.proxyaddresses[0]}}, `
@{L='ProxyAddress_2';E={$_.ProxyAddresses[1]}}, `
@{L='ProxyAddress_3';E={$_.ProxyAddresses[2]}}, `
@{L='ProxyAddress_4';E={$_.ProxyAddresses[3]}}, `
@{L='ProxyAddress_5';E={$_.ProxyAddresses[4]}}, `
@{L='ProxyAddress_6';E={$_.ProxyAddresses[5]}}, `

Export-Csv -Path "Your path\Your filename.csv" -NoTypeInformation -Encoding UTF8 -Delimiter "|"

I am using | as a delimiter so I don't have to care if there is a , or a ; in the data I export. Then changed the addresses with the replace command and set the users properties.

$Temp = Import-Csv -Path "Your path\Your filename.csv" -Encoding Default -Delimiter '|'
ForEach ($User in $Temp) {
Set-ADUser -Identity $User.SamAccountName -Clear proxyaddresses
Set-ADUser -Identity $User.SamAccountName -Add @{proxyAddresses = $User.ProxyAddress_1}
Set-ADUser -Identity $User.SamAccountName -Add @{proxyAddresses = $User.ProxyAddress_2}
Set-ADUser -Identity $User.SamAccountName -Add @{proxyAddresses = $User.ProxyAddress_3}
Set-ADUser -Identity $User.SamAccountName -Add @{proxyAddresses = $User.ProxyAddress_4}
Set-ADUser -Identity $User.SamAccountName -Add @{proxyAddresses = $User.ProxyAddress_5}
}

So the Set-ADUser command is working but you have to put the -Add and set that value to the multivalued attribute.

Hope this helps you.

Edit: Also you can see the -Clear command before adding new values because if you have too many mail aliases in one user you may find yourself in a difficult situation.

After several tries I got this working in order to replace wrong characters in the proxuAddresses array attribute:

$pa = Get-ADObject "$idistinguishedname" -Properties proxyAddresses | select proxyAddresses
$pa | foreach {If ($iVALUE -ceq $pa[0]){Set-ADObject "$idistinguishedname" -replace @{proxyAddresses=$iUPDATE}}}

$iVALUE and $iUPDATE came as input from a CSV file that I exported and carefully edited after running IdFix tool vs my AD in order to fix inconsistencies. IdFix tool should have done this but it errored out and had to code a PS script to finish what the tool couldn't.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM