none
Upding ProxyAddresses through PutEx

    Question

  • Hello Team,
    Was working on a script to update bulk email addresses, to replace -ext out of the primary email address. We have multiple proxyaddresses.
    For Example: User ABC has following proxy addresses
     SMTP:ABC-Ext@contoso.com
     smtp:abctest-ext@contoso.com
     smtp:test.l54shared@contoso.com
     X400:C=US;A=NYC;P=CONTOSO;O=DOMAIN;G=Test;I=TL;
    I'm just replacing the ABC-Ext@contoso.com to ABC@contoso.com primary address and also adding the ABC-Ext@contoso.com as secondary.
    My Code looks like this (this is indeed the big script but I'm just posting a chunk of it)
    function Set-EmailAddresses([System.DirectoryServices.DirectoryEntry] $object, [string] $sProperty, $oValue)
    {
     $ads_property_append = 3
         if ($oValue -ne $null)
            {$object.PutEx($ads_property_append, $sProperty, $oValue)}
      $object.proxyaddresses
            }
    # Add Exchange Shell...
    If((Get-PSSnapin | where {$_.Name -match "Microsoft.Exchange.Management.PowerShell.E2010"}) -eq $Null)
    {
        Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
    }
    #Static Variables
    #add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010
    $dc = "ADCONDC.contoso.com"
    $mbx = "ABC-Ext@contoso.com"
    $dn =(Get-Mailbox -DomainController "$dc" -identity $mbx).distinguishedname
    $mailbox = New-Object System.DirectoryServices.DirectoryEntry("LDAP://$dc/"+$dn)
    $Primary. = @("SMTP:" + ($mailbox.mail -replace "-EXT"))

    Set-EmailAddresses $mailbox "ProxyAddresses" ([Array] $Primary)
    #$mailbox.proxyaddresses

    Once it is executed, the ProxyAddresses will have only SMTP:ABC@contoso.com and will have other proxy addresses wiped. Could you please help me in this?

    Never giveup till you get what to want.

    Saturday, November 11, 2017 4:00 AM

Answers

  • Non-programmers always add lines of useless code.  I simplified your code to what is needed.  The first rule of programming is never write a line of code that you do not need and never add code you do not understand.  This makes the code easier to understand,  less prone to errors and easier to debug.

    This is all you need:

    Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 -EA 0
    $mb = Get-Mailbox -DomainController ADCONDC.contoso.com -identity ABC-Ext@contoso.com
    $dn = $mb.distinguishedname
    Try{
        $user = [adsi]"LDAP://$($mb.distinguishedname)"
        $primary = "SMTP:" + $user.mail -replace '-EXT'
        $user.PutEx(3, 'ProxyAddresses', @($primary))
        $user.SetInfo()
    }
    Catch{
        Throw $_
    }

    What you posted still does not match what you are asking and what you are asking is very vague.


    \_(ツ)_/


    • Edited by jrv Saturday, November 11, 2017 6:42 AM
    • Marked as answer by Ranji Rock Saturday, November 11, 2017 9:16 AM
    Saturday, November 11, 2017 6:34 AM

All replies

  • Please post you code with the code posting tool so that it is readable.

    You should be using the Set-AdUser CmdLet.

    help Set-AdUser -online

    Here is how to get and reset proxyaddresses:

    $u = Get-aduser jvierra -prop proxyaddresses
    $addresses=$u.Proxyddresses
    # now fix the array and send it back
    Set-aduser jsmith-Clear ProxyAddresses
    Set-aduser jsmith-Add @{ProxyAddresses = $addresses}
    

    That is how easy it is.


    \_(ツ)_/


    • Edited by jrv Saturday, November 11, 2017 4:52 AM
    Saturday, November 11, 2017 4:39 AM
  • Sorry for that. Yes, this way it works

    But is there a way we can do it through PutEx in a separate function? Because, we already have a script which update multiple attributes, but for multivalued attributes like Proxyaddresses update function was not there, and hence we are creating it. Could you help me with the function which is there in my script and let me know what is wrong in that please?


    Never giveup till you get what to want.

    Saturday, November 11, 2017 4:48 AM
  • Look in the Gallery for scripts that update the proxyaddreses attribute.  There are quite a few.

    It is tricky. If you make a mistake the mailbox will become unusable.  I recommend using the CmdLets.


    \_(ツ)_/

    Saturday, November 11, 2017 4:54 AM
  • $user.Putex(1 , 'proxyaddresses') # clear addresses
    $user.setinfo()

    $user.Putex(2, 'proxyaddresses',  @('smtp:jjones@msn.com',mdoe@msn.com'))
    $user.setinfo()

    1 = clear
    2 = update
    3 = append
    4 = delete


    \_(ツ)_/



    • Edited by jrv Saturday, November 11, 2017 5:01 AM
    Saturday, November 11, 2017 4:58 AM
  • I did search all these on internet, but non of them fixed my script problem:(

       $aduser = get-aduser -identity xxxxxxx

                    #    append value to proxyaddress array using ADSI Edit connector
                    $user = [ADSI]"LDAP://$($aduser.distinguishedname)"
                    $ads_property_append = 3
                    $user.Putex($ads_property_append, "proxyaddresses",  @("smtp:$($aduser.emailaddress)"))
                    $user.setinfo()

    The above code works perfectly. But when I use it inside a function it is wiping of all the addresses and keeping only one address, so I need someone to help me on that. As already told, I have a big script to modify multiple attributes, now I'm just adding another function to update the multi-valued attribute. Please help me on this.


    Never giveup till you get what to want.

    Saturday, November 11, 2017 4:59 AM
  • What is wrong with my coding? I need to understand and fix that so that I can include this function in the existing script we have. Thank you

    Never giveup till you get what to want.

    Saturday, November 11, 2017 5:00 AM
  • You can also do this:

    $user.ProxyAddresses = @( ... array of addresses ...)|
    $user.SetInfo()


    \_(ツ)_/

    Saturday, November 11, 2017 5:06 AM
  • Yes, I'm trying to append (3). It works if I use directly in main function.
    But, if I pass through function, it is wiping off other address, is there something wrong?

    Never giveup till you get what to want.

    Saturday, November 11, 2017 5:12 AM
  • What is wrong with my coding? I need to understand and fix that so that I can include this function in the existing script we have. Thank you

    Never giveup till you get what to want.

    First post you code so that it is readable and can be copied correctly.

    If you corrupt the addresses then you will have to clear them first.  With Exchange running the addresses acnnot be updated if there is an error in the format or a conflict between addresses.  Exchange monitors and validates the addresses.

    Set-AdUser userid -Clear ProxyAddresses


    \_(ツ)_/

    Saturday, November 11, 2017 5:13 AM
  • As you requested, copying from Powershell ISE

    function Set-EmailAddresses([System.DirectoryServices.DirectoryEntry] $object, [string] $sProperty, $oValue)
    {
     $ads_property_append = 3
         if ($oValue -ne $null)
            {$object.PutEx($ads_property_append, $sProperty, $oValue)}
             $object.proxyaddresses
             $object.setinfo()
    }
    
    # Add Exchange Shell...
    If((Get-PSSnapin | where {$_.Name -match "Microsoft.Exchange.Management.PowerShell.E2010"}) -eq $Null)
    {
        Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
    }
    
    $dc = "ADCONDC.contoso.com"
    $mbx = "ABC-Ext@contoso.com"
    $dn =(Get-Mailbox -DomainController "$dc" -identity $mbx).distinguishedname
    $mailbox = New-Object System.DirectoryServices.DirectoryEntry("LDAP://$dc/"+$dn)
    $Primary = @("SMTP:" + ($mailbox.mail -replace "-EXT"))
    Set-EmailAddresses $mailbox "ProxyAddresses" ([Array] $Primary)


    Never giveup till you get what to want.


    • Edited by Ranji Rock Saturday, November 11, 2017 6:28 AM
    Saturday, November 11, 2017 5:58 AM
  • Please look at what you just pasted.  I think you made a mistake,


    \_(ツ)_/

    Saturday, November 11, 2017 6:03 AM
  • I have posted it right in my last post.
    It is producing the result. But, updating only one address, and wiping out previous proxyaddresses

    Never giveup till you get what to want.

    Saturday, November 11, 2017 6:08 AM
  • Look at your last post.  You posted it twice,  nce badly and th second time better.


    \_(ツ)_/

    Saturday, November 11, 2017 6:15 AM
  • I have posted it right in my last post.
    It is producing the result. But, updating only one address, and wiping out previous proxyaddresses

    Never giveup till you get what to want.


    You know that you can edit your original post.  That is where the readable code needs to be., Consider others who may want to use this info.  The purpose of these forums is to provide worked out examples of how to do scripting. Unreadable questions do not make that easy.

    \_(ツ)_/

    Saturday, November 11, 2017 6:20 AM
  • sorry for that, I have corrected it now. Please help

    Never giveup till you get what to want.

    Saturday, November 11, 2017 6:28 AM
  • Non-programmers always add lines of useless code.  I simplified your code to what is needed.  The first rule of programming is never write a line of code that you do not need and never add code you do not understand.  This makes the code easier to understand,  less prone to errors and easier to debug.

    This is all you need:

    Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 -EA 0
    $mb = Get-Mailbox -DomainController ADCONDC.contoso.com -identity ABC-Ext@contoso.com
    $dn = $mb.distinguishedname
    Try{
        $user = [adsi]"LDAP://$($mb.distinguishedname)"
        $primary = "SMTP:" + $user.mail -replace '-EXT'
        $user.PutEx(3, 'ProxyAddresses', @($primary))
        $user.SetInfo()
    }
    Catch{
        Throw $_
    }

    What you posted still does not match what you are asking and what you are asking is very vague.


    \_(ツ)_/


    • Edited by jrv Saturday, November 11, 2017 6:42 AM
    • Marked as answer by Ranji Rock Saturday, November 11, 2017 9:16 AM
    Saturday, November 11, 2017 6:34 AM
  • This is the key:

    $primary = "SMTP:" +$user.mail -replace '-EXT'
    $user
    .PutEx(3, 'ProxyAddresses', @($primary))

    You will get an error if another address is set as primary.


    \_(ツ)_/

    Saturday, November 11, 2017 6:43 AM
  • The easy way is to just do this:

    Set-Mailbox -PrimaryAddress user@domain.com

    This does everything in one step and will reset the primary address.


    \_(ツ)_/

    Saturday, November 11, 2017 6:49 AM
  • I understand sir, but I cannot make this is main program.
    My requirement is to add a function which does this. I used PutEx (Append 3) in the Function which is not working.
    I don't know why it is not appending if I use it inside the function.


    Never giveup till you get what to want.

    Saturday, November 11, 2017 7:01 AM
  • Once you learn enough PowerShell and ADSI you can then create a function.  Right now you need to find out how to do this.  I am not here as a free consulting service.  I have been doing this for 30+ years.  If you follow my instructions you will learn the answer.

    The code I posted was tested on my server and works as posted.  It does not handle every possible condition.  That will be your responsibility once you understand how the code works.

    Make the code work then worry about some odd rule that says you have to have a function.  You are not a programmer.  Why do you think that rule is so important? 


    \_(ツ)_/

    Saturday, November 11, 2017 7:17 AM
  • Thank You

    Never giveup till you get what to want.

    Saturday, November 11, 2017 9:15 AM