Powershell code snippet to change the capital 'SMTP' proxyaddress domain suffix from abc.com to xyz.com

Biju Thankappan 86 Reputation points
2021-09-07T12:42:05.103+00:00

Hi,

I have this script to replace the domain suffix for contacts having their capital SMTP address in proxyaddress' list:

$f = 'users1.csv'

$ou = 'OU=Test,OU=Test,OU=External Contact Records,OU=Exchange,DC=xyz,DC=com'

Import-Csv -Path $f | ForEach-Object {

    $CN = $_.Contacts 

    Get-ADObject -Filter {(objectClass -eq "contact") -and (mail -like '*abc.com')} -SearchBase $ou -Properties targetAddress,sn,givenName,proxyAddresses,mailNickname | ForEach-Object{

    $pas = @($_.Proxyaddresses)

    foreach ($pa in $pas) {

        if($pa -cmatch '^SMTP') {

        $NewPAddress = $pa.Replace('abc','xyz')

}

}

        Set-ADObject -Identity $_ -Replace @{Proxyaddresses=$NewPAddress} 

} }

However, the problem with this is....it replaces the other entries and only adds this one entry in the proxyaddress' list for the AD Contact.

How can I ensure the other entries remain there as is and only replaces this one entry with the correct domain suffix for the contact?

Any help is highly appreciated

TIA

Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
5,945 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,390 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 45,096 Reputation points
    2021-09-07T14:34:05.487+00:00

    Is it safe to assume that you're using MS Exchange, and that what you really want to change is the contact's external email address? If so, why not use the Get-MailContact (using the -Filter parameter) and pipe the result to Set-MailContact? Use the -ExternalEmailAddress parameter to do this and you'll change all the right properties (not just the primary SMTP proxy address).

    If you choose to use your current script, the ProxyAddresses property is a collection of addresses. I believe you need to modify the existing set of addresses and then replace them as a whole array.


  2. Rich Matheisen 45,096 Reputation points
    2021-09-08T01:55:30.527+00:00

    You can try this:

    $f = 'users1.csv'
    
    $ou = 'OU=Test,OU=Test,OU=External Contact Records,OU=Exchange,DC=xyz,DC=com'
    Import-Csv -Path $f | 
        ForEach-Object {
            $CN = $_.Contacts       # the $CN variable is never used!
            Get-ADObject -Filter { (objectClass -eq "contact") -and (mail -like '*abc.com') } -SearchBase $ou -Properties targetAddress, sn, givenName, proxyAddresses, mailNickname | 
                ForEach-Object {
                    $pas = @()
                    foreach ($pa in $_.ProxyAddresses) {
                        if ($pa -cmatch '^SMTP') {
                            $pas += $_.Replace('abc', 'xyz')
                        }
                        else{
                            $pas += $_
                        }
                    }
                    Set-ADObject -Identity $_ -Replace @{Proxyaddresses = $pas} 
                }
        }
    

    I don't have an Exchange organization so I can't verify the code. You should check to make sure the proxy addresses are just a simple string because they may not be! You may also have to alter other properties of the contact object in order for them to function properly because changing the proxy addresses array may not change things like the primary SMTP proxy address.