none
Create new primary proxy address; retain original as secondary RRS feed

  • Question

  • Hello,

    I need to create a new primary proxy address while retaining any old addresses as secondary. I have seen this questioned answered before but those scripts use commands only available on Exchange Server and I have to do this on Windows Server 2008 R2.

    I am able to generate and insert the new proxy address but I end up with two primary, or at least they are both upper case SMTP. I tried a couple scripts to -replace SMTP with smtp but they throw various errors.

    I was going to go the route of creating a .csv file and replacing strings of SMTP but not every user has the same number of old proxy addresses and I can't seem to write the Get-ADUser statement that allows for a multivariable field that may be null.

    This is what I have. What works is that I create the new primary proxyAddress, inserts it, but the old one stays as a capitalized SMTP address as well. I just need the old one(s) to go to lower case.

    Import-Module activedirectory
    $newproxy = '@domain.org'
    $Smtp='SMTP:'
    $userou   = 'OU=test,DC=domain,DC=org'
    $users    = Get-ADUser -Filter * -SearchBase $userou -Properties ProxyAddresses 
    
    Foreach ($user in $users) 
    {
            if ($user.GivenName -and $user.Surname) {
            $fname=$($user.givenname[0])
            $lname=$($user.surname)
            $proxyaddresses=$Smtp + $fname + $lname + $newproxy
            $email=$fname + $lname + $newproxy
            Set-ADUser -Identity $user.samaccountname -add @{proxyaddresses = $proxyaddresses}
            Set-ADUser -Identity $user.samaccountname -email $email
        }
         else {
            Write-Warning "User '$($user.SamAccountName)' has incomplete Name info"
        }
        
        }

    Tuesday, April 9, 2019 10:28 PM

Answers

  • Using your code this would be how to change the primary without removing the old address.

    Get-ADUser -Filter * -SearchBase 'OU=test,DC=domain,DC=org' -Properties ProxyAddresses |
        ForEach-Object{
            if ($_.GivenName -and $_.Surname) {
                $emailAddress = '{0}{1}@domain.org' -f $_.GivenName, $_.Surname
                $SmtpPrimary = 'SMTP:{0},' -f $emailAddress
                $newProxy = $SmtpPrimary + $_.ProxyAddresses.ToLower()
                $_ | Set-ADUser -replace @{ proxyaddresses = $newProxy } -EmailAddress $emailAddress
            } else {
                Write-Warning "User '$($user.SamAccountName)' has incomplete Name info"
            } 
        }

    WARNING: if you have screwed around with the addresses and corrupted them then this may throw errors on the bad addresses.  This is why all of us with years of experience recommend using the Exchange CmdLets.


    \_(ツ)_/


    • Edited by jrv Wednesday, April 10, 2019 6:07 PM
    • Marked as answer by Alewis6415 Wednesday, April 10, 2019 6:29 PM
    Wednesday, April 10, 2019 6:06 PM

All replies

  • Please look in the Gallery for scripts that do what you ask.  It is tricky and a failure can create big issues with Exchange.  I strongly recommend that you use the Exchange CmdLets for this as they can prevent errors.


    \_(ツ)_/

    Tuesday, April 9, 2019 11:05 PM
  • Thank you,

    I don't think using Exchange Cmdlets ia an option. We sync with Office 365 from our AD.

    Tuesday, April 9, 2019 11:11 PM
  • ... I tried a couple scripts to -replace SMTP with smtp but they throw various errors.  ...

    -replace works case-insensitiv by default. If you like to change this you'll have to use -creplace.

    ... and if you get errors you should debug the code instead of just giving up. ;-) You might post the error messages here so we might be able to help.


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Tuesday, April 9, 2019 11:14 PM
  • Thank you,

    I don't think using Exchange Cmdlets ia an option. We sync with Office 365 from our AD.

    The O356 Cmdlets ort the Exchange online shell can do this.

    In any case the answer is to use one of the scripts in the Gallery. This question is asked about once a month and the answer is mostly to use one of the existing scripts.

    Changing "ProxyAddresses" in AD will not necessarily update O365.


    \_(ツ)_/

    Wednesday, April 10, 2019 12:08 AM
  • Thank you,

    I don't think using Exchange Cmdlets ia an option. We sync with Office 365 from our AD.

    The O356 Cmdlets ort the Exchange online shell can do this.

    In any case the answer is to use one of the scripts in the Gallery. This question is asked about once a month and the answer is mostly to use one of the existing scripts.

    Changing "ProxyAddresses" in AD will not necessarily update O365.


    \_(ツ)_/

    If one of the existing scripts worked for my needs I would have used them. Most of them assume that you're using Exchange. I will keep digging though, thank you.
    Wednesday, April 10, 2019 4:21 PM
  • If one of the existing scripts worked for my needs I would have used them. Most of them assume that you're using Exchange. I will keep digging though, thank you.

    Almost none of the scripts I referenced require Exchange. Almost all just use the AD CmdLets. You need to put in more effort. We are not going to write a custom script and I am not going to do your searching for you since you won't even look at the scripts.


    \_(ツ)_/

    Wednesday, April 10, 2019 4:24 PM
  • If you just search for this string - "set-aduser proxyaddesses" - you will find hundreds of examples.


    \_(ツ)_/

    Wednesday, April 10, 2019 4:26 PM
  • If one of the existing scripts worked for my needs I would have used them. Most of them assume that you're using Exchange. I will keep digging though, thank you.

    Almost none of the scripts I referenced require Exchange. Almost all just use the AD CmdLets. You need to put in more effort. We are not going to write a custom script and I am not going to do your searching for you since you won't even look at the scripts.


    \_(ツ)_/

    The script I posted above is from the Gallery and I modified it as needed. So I have clearly Searched or I wouldn't have had that script to post. I don't know why you are making assumptions like "I won't even look at the scripts". If the total sum of your advice is to "Search the Gallery" then don't waste your time, it's not helpful, nobody is posting questions without having search for an answer already.
    Wednesday, April 10, 2019 5:16 PM
  • And what is it that you don't like bout the script?

    There are scripts in the Gallery that will replace the primary, add a new address or remove an address.  Your script does not do any of that.  It just adds a primary address.  You have to either replace the primary or convert it to a non-primary.  Do this by copying all addresses to an array. Cha ging the array to suite your needs and add it back after clearing the original attribute.


    \_(ツ)_/

    Wednesday, April 10, 2019 5:41 PM
  • Using your code this would be how to change the primary without removing the old address.

    Get-ADUser -Filter * -SearchBase 'OU=test,DC=domain,DC=org' -Properties ProxyAddresses |
        ForEach-Object{
            if ($_.GivenName -and $_.Surname) {
                $emailAddress = '{0}{1}@domain.org' -f $_.GivenName, $_.Surname
                $SmtpPrimary = 'SMTP:{0},' -f $emailAddress
                $newProxy = $SmtpPrimary + $_.ProxyAddresses.ToLower()
                $_ | Set-ADUser -replace @{ proxyaddresses = $newProxy } -EmailAddress $emailAddress
            } else {
                Write-Warning "User '$($user.SamAccountName)' has incomplete Name info"
            } 
        }

    WARNING: if you have screwed around with the addresses and corrupted them then this may throw errors on the bad addresses.  This is why all of us with years of experience recommend using the Exchange CmdLets.


    \_(ツ)_/


    • Edited by jrv Wednesday, April 10, 2019 6:07 PM
    • Marked as answer by Alewis6415 Wednesday, April 10, 2019 6:29 PM
    Wednesday, April 10, 2019 6:06 PM
  • Here is a module that does this in a more robust way:

    Install-Module ProxyAddressUpdate


    \_(ツ)_/

    Wednesday, April 10, 2019 6:13 PM