locked
Update ProxyAddresses based on mail attribute RRS feed

  • Question

  • Hi all,

    Some of our users have double primary SMTP address in proxyaddresses and we would like to correct it, so basically the primary SMTP should match mail attribute and any other address in proxyaddresses should be set to secondary smtp (lower case)

    I managed to write following script:

    $users = Get-Content .\Desktop\smtp1.txt

    #users with proxy and mail attributes
    foreach ($user in $users) {
    $aduser=Get-ADUser -Identity $user -Properties mail,ProxyAddresses
    foreach ($proxyaddress in $aduser.ProxyAddresses) {
    $proxy=$aduser.ProxyAddresses
    if ($proxyaddress.StartsWith("SMTP:") -and $proxyaddress -eq "SMTP:$($aduser.mail)"){set-aduser -identity $user -replace @{'proxyAddresses'=$proxy.ToUpper()}}
            elseif (($proxyaddress.StartsWith("SMTP:") -and $proxyaddress -ne "SMTP:$($aduser.mail)") ){set-aduser -identity $user -replace @{'proxyAddresses'=$proxy.ToLower()}}
            }
    }

    but it still doesn't update the proxyaddresses as described above. 

    Could you please give me a hint?

    Thank you!

    Wednesday, March 14, 2018 3:47 PM

Answers

  • Hi,

    According to your situation, you may not have to use Function to achieve the requirement. The following example script for your reference, please test it for a test user first to see if it works:
    Get-Content -Path .\Desktop\smtp1.txt | ForEach-Object {
        $user = Get-ADUser -Identity $_ -Properties mail, proxyAddresses
        $proxy = $user.proxyAddresses | ForEach-Object {
            if ($_ -match $user.mail) {
                $_.ToLower() -replace 'smtp','SMTP'
            }
            else {
                $_.ToLower()
            }
        }
        Set-ADUser -Identity $user -Replace @{proxyAddresses = $proxy}
    }

    If you need further help, please feel free to let us know.

    Best Regards,
    Albert

    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Tuesday, March 20, 2018 9:12 AM

All replies

  • Please use the code pasting tool so that we can read your code easier and also copy it for testing.

    Are you getting any warning or error messages?


    Learn PowerShell

    Script Requests

    -Remember to mark the correct response as the answer-

    Wednesday, March 14, 2018 5:11 PM
  • Here the code once again:

    $users = Get-Content .\Desktop\smtp1.txt
    
    #users with proxy and mail attributes
    foreach ($user in $users) {
    $aduser=Get-ADUser -Identity $user -Properties mail,ProxyAddresses
    foreach ($proxyaddress in $aduser.ProxyAddresses) {
    $proxy=$aduser.ProxyAddresses
    if ($proxyaddress.StartsWith("SMTP:") -and $proxyaddress -eq "SMTP:$($aduser.mail)"){set-aduser -identity $user -replace @{'proxyAddresses'=$proxy.ToUpper()}}
            elseif (($proxyaddress.StartsWith("SMTP:") -and $proxyaddress -ne "SMTP:$($aduser.mail)") ){set-aduser -identity $user -replace @{'proxyAddresses'=$proxy.ToLower()}}
            }
    } 

    It doesn't give any error, but somehow the set-aduser part changes all proxyaddresses to lower case,



    • Edited by AN_beginner Wednesday, March 14, 2018 8:52 PM
    Wednesday, March 14, 2018 7:07 PM
  • You code is not formatted in any readable way.

    I recommend looking in the Gallery for scripts that edit the proxy addresses.

    The first step is to understand that it is an array of addresses which have to be expanded to work with.

    $p = $aduser.ProxyAddesses
    # adjust address array
    $p = @(
    'smtp:jsmith@gmail.com',
    'SMTP:joe@gmail.com',
    'joe@gmail.com'
    )
    # first clear then reset the array
    Set-AdUser jsmith -Clear ProxyAddresses
    Set-AdUser jsmith -Add @{ProxyAddresses=$p}


    \_(ツ)_/

    Wednesday, March 14, 2018 7:26 PM
  • Here is how to alter an address in the array.

    $p |Where{ $_ -cmatch 'SMTP:'} | ForEach{$_ = 'SMTP:newuser@domain.com'}

    This finds and changes the default address.

    I recommend either changing that first then processing the remainder or setting all to lowercase.

    $p |Where{ $_ -cmatch 'SMTP:'} | ForEach{$_ = $_.ToLower()}

    That will remove all primaries. now just find the user replace with upper SMTP.

    $p |Where{ $_ -match $userid} | ForEach{$_ =  $_ -replace 'smtp','SMTP'}

    You can also use Exchange CmdLets to more directly adjust these.

    Also avoid large, complex if/else structures as they will add confusion.  If you are not a programmer well versed in logic if/else logic will cause many issues.  Just do simple tests and steps for each rule you want to apply.


    \_(ツ)_/


    Wednesday, March 14, 2018 7:42 PM
  • Thank you for the hint.

    Just to be sure. I want to keep the other proxyaddresses (the secondary ones), so is there a possibility that the array will remove the other ones that don't match email?

    I have to admit that I don't know that method (I'm a beginner) so how could I compose it with the current code? Just replace the if part?

    I was looking for a solution to update all the addresses in one go, hence that if construction, but if it's not possible, then I'll give it a try.

    Wednesday, March 14, 2018 8:08 PM
  • Where is anything being removed from the array?  The array is just having the addresses changed to reflect the correct primary and remove the duplicate.

    Take it one piece at a time.  Don't clear and reset the addresses until you are sure the array is being updated correctly.

    You are in a territory that requires programming skills.  If you have no programming experience you will want t get some so you can understand what is happening.


    \_(ツ)_/

    Wednesday, March 14, 2018 8:22 PM
  • While this reply doesn't directly address your problem (i.e. it's not "PowerShell Specific"), you can avoid this sort of problem by not using the AD Users And Computers snap-in, or Set-ADUser, to modify e-mail attributes. You should also avoid directly modifying the proxyAddresses property using code unless you're sure of what you're doing. Duplicate primary proxy address should never be caused by using an Exchange admin snap-in, or the Exchange Powershell cmdlets -- but I've seen lots of problems like yours when the correct tools are not used to manage Exchange objects.

    In other words, stop using cmdlets such as Set-ADUser and start using Set-Mailbox, Set-MailUser, and Set-Contact to manage Exchange objects.

    This is an oldie, but a goodie, and explains some of what goes on behind the scenes when you change "stuff":
    https://blogs.technet.microsoft.com/exchange/2005/01/10/fun-with-changing-e-mail-addresses/


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Wednesday, March 14, 2018 8:40 PM
  • While this reply doesn't directly address your problem (i.e. it's not "PowerShell Specific"), you can avoid this sort of problem by not using the AD Users And Computers snap-in, or Set-ADUser, to modify e-mail attributes. You should also avoid directly modifying the proxyAddresses property using code unless you're sure of what you're doing. Duplicate primary proxy address should never be caused by using an Exchange admin snap-in, or the Exchange Powershell cmdlets -- but I've seen lots of problems like yours when the correct tools are not used to manage Exchange objects.

    In other words, stop using cmdlets such as Set-ADUser and start using Set-Mailbox, Set-MailUser, and Set-Contact to manage Exchange objects.

    This is an oldie, but a goodie, and explains some of what goes on behind the scenes when you change "stuff":
    https://blogs.technet.microsoft.com/exchange/2005/01/10/fun-with-changing-e-mail-addresses/


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    This is all true but does not affect Exchange 2010 Update 3 and later as MS added guards.  In earlier versions duplicate primary designators would cause havoc.  I suspect the OP is suing an unpatched version of Exchange 2010 or the dupes were there from before.  In the updated versions Exchange only accepts the first primary designation and ignore others.  If the mail is working this can be ignored.  If someone reorders the addresses then it may be an issue. In the newer versions AD will throw an exception when a dupe is assigned and the assignment will fail.  Adding an address also causes a failure I believe but forget what I tested way back then.

    A single pass of the above code that just sets the correct address would be a quick fix.  Opening the addresses in the Exchange console and saving should also re move the second primary designation.

    Using the Exchange CmdLets and setting the primary will also fix the array.

    In all of my installations all addresses are managed by the address generator.  We give everyone one option besides the company standard ... an alias like a first name or other simple secondary name manipulation as long as it is unique.  For alternate domains we allow one usually for marketing or upper management but I discourage creativity with mail addresses.

    If the primary addresses are all standard formats like first initial last name then a single pass with Exchange CmdLets should fix the dupes.

    Get-AdUser -Filter * | %{Set-Mailbox -PrimarySmtpAddress  (build address from user)

    Mail address policies can also fix this in Exchange 2013 and later.  I don't remember if they could in 2010.


    \_(ツ)_/

    Wednesday, March 14, 2018 9:09 PM
  • Thank you for all hints. In our setup proxyaddresses have to be updated in AD.

    I know that the update via Exchange is much easier.

    I was searching in the Microsoft Gallery for similar scripts but basically I could find only scripts to add new values to proxyaddresses,

    As I mentioned I'm a beginner so that's why I asked you for help :-)

    Thursday, March 15, 2018 6:02 PM
  • Hi,

    I'm checking how the issue is going, was your issue resolved?

    And if the replies as above are helpful, we would appreciate you to mark them as answers, and if you resolve it using your own solution, please share your experience and solution here. It will be greatly helpful to others who have the same question.

    Appreciate for your feedback.

    Best Regards,
    Albert


    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Monday, March 19, 2018 9:17 AM
  • Hi Albert,

    I'm still testing different solutions. As mentioned the value has to be updated in AD.

    Would someone be so kind and check if below script makes sense? Is it moving in the right direction? Any hints would be much appreciated.

    Below the code. I didn't test it yet.

    Import-Module ActiveDirectory
    
    Function Get-ADUser {
    Param(
    $users = (Get-Content .\Desktop\smtp1.txt),
    $aduser,
    $proxy=$aduser.ProxyAddresses,
    $mail=$aduser.mail
    )
    
    $aduser=Get-ADUser -Identity $user -Properties mail,ProxyAddresses
    }
    
    foreach ($user in $users) {
    $aduser=Get-ADUser
    foreach ($proxyaddress in $aduser.ProxyAddresses) {
    
    $proxy |Where{ $_ -cmatch 'SMTP:'} | ForEach{$_ = $_.ToLower()}}
    }
    
    foreach ($user in $users) {
    $aduser=Get-ADUser
    foreach ($proxyaddress in $aduser.ProxyAddresses) {
    $proxy |Where{ $_ -match $mail} | ForEach{$_ =  $_ -replace 'smtp','SMTP'}}
    }


    Monday, March 19, 2018 2:01 PM
  • Hi,

    According to your situation, you may not have to use Function to achieve the requirement. The following example script for your reference, please test it for a test user first to see if it works:
    Get-Content -Path .\Desktop\smtp1.txt | ForEach-Object {
        $user = Get-ADUser -Identity $_ -Properties mail, proxyAddresses
        $proxy = $user.proxyAddresses | ForEach-Object {
            if ($_ -match $user.mail) {
                $_.ToLower() -replace 'smtp','SMTP'
            }
            else {
                $_.ToLower()
            }
        }
        Set-ADUser -Identity $user -Replace @{proxyAddresses = $proxy}
    }

    If you need further help, please feel free to let us know.

    Best Regards,
    Albert

    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Tuesday, March 20, 2018 9:12 AM
  • Hi Albert,

    yes! that's exactly what I needed.

    Thank you for your help!

    Wednesday, March 21, 2018 9:23 AM