Remove Old smtp proxy addresses from 1000's of contacts

Hi All!

I’ve come across and issue where an email advisory addin to outlook is picking up on old smpt proxy addresses that are left over from our migration to exchange online. The app is supposed to warn users when they are emailing an external contact. We’ve discovered that this Addin is not longer recognising exchange contacts that still have an automatically created company smtp address.

I’ve kludged together a script that I’m having some difficulty with and any help would be appreciated:

$Users=Get-ADObject -SearchBase "OU=OU1,OU=OU2,OU=OU3,OU=OU4,DC=DC,DC=DC" `
-filter "proxyAddresses -like '*@Company*'" -properties proxyaddresses | Select DistinguishedName, @{name='smtp'; expression={($_.ProxyAddresses | Where-Object {$_ -like "*@Company*" })}}

Foreach ($User in $Users) 
{
Set-ADObject -Identity $user.DistinguishedName -Remove @{Proxyaddresses="$($user.smtp)"}
}

The Search part works fine but I can’t get the removal part to work. I think the problem lies with needing to use the Get-ADObject instead of the Get-ADUser the script was made for.

Any help is appreciated :slight_smile:
Jacob

Jacob,
Welcome to the forum. :wave:t4:

Before we proceed … when you post code or sample data or console output please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.
Without that the forum software tries to interpret your code and may remove some characters like dollar signs. :wink:
Thanks in advance

That’s unfortunately not that helpful. What does that mean? Do you get error messages?

Why do you think that? And why don’t you use Get-ADUser and Set-ADUser?

Hi Olaf Thanks for your reply.

I’m forced to use the Get-ADObject command because the objects I’m trying to edit are not users and didn’t show up when using the Get-ADUser command.

There is no error message when running the script and I think it may be due to using the DistinguishedName as the identifier for the contacts.

I can’t get the remove command to work when I do this manually:

Set-ADObject -Identity "DistinguishedName" -Remove @{Proxyaddresses=smtp:"fullproxyaddress"}

But when I use the ObjectGUID it will remove the Proxyaddress:

Set-ADObject -Identity ObjectGUID -Remove @{Proxyaddresses=smtp:"fullproxyaddress"}

Unfortunately this won’t accept the ObjectGUID as a property:

$Users=Get-ADObject -SearchBase "OU=OU1,OU=OU2,OU=OU3,OU=OU4,DC=DC,DC=DC" `
-filter "proxyAddresses -like '*@Company*'" -properties proxyaddresses | Select ObjectGUID, @{name='smtp'; expression={($_.ProxyAddresses | Where-Object {$_ -like "*@Company*" })}}

Hmmm … I just tried it and this worked for me:

$SearchBase = 'OU=OU1,OU=OU2,OU=OU3,OU=OU4,DC=DC,DC=DC'
$ContactList = Get-ADObject -LDAPFilter 'objectClass=Contact' -SearchBase $SearchBase -Properties ProxyAddresses |
    Where-Object { $_.ProxyAddresses -like '*@Company*' } |
        Select-Object *, @{name = 'smtp'; expression = { ($_.ProxyAddresses | Where-Object { $_ -like '*@Company*' }) } }
foreach ($Contact in $ContactList) {
    Set-ADObject -Identity $($Contact.DistinguishedName) -Remove @{ProxyAddresses = $($Contact.smtp) }
}

Cool! Thank you!
I’ll give this a go now!

I Get this error:

Set-ADObject : Invalid type 'System.Management.Automation.PSObject'.
Parameter name: ProxyAddresses
At line:6 char:5
+     Set-ADObject -Identity $($Contact.DistinguishedName) -Remove @{Pr ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (CN=mgbs,OU=Exte...=Pharmac,DC=dom:ADObject) [Set-ADObject], ArgumentException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Commands.SetADObject

Any Ideas?

I Got it Sorted thank you!

There were multiple proxyaddresses that matched that search outcome.
When I Specified the search parameters so that it would only do each one it worked!

Thank you SOO much for your help!

So I do have one request if you are able to help.

Can you help me get this script running for multiple proxyaddresses per contact that fit the same search criteria?

Thanks in advance!

Hmmm … very strange … it seems not to work at all when you provide more than one smtp address at a time. So you may remove them one by one. It’ll increase the runtime. But at least it does the job. :wink:

$SearchBase = 'OU=OU1,OU=OU2,OU=OU3,OU=OU4,DC=DC,DC=DC'
$ContactList = 
    Get-ADObject -LDAPFilter 'objectClass=Contact' -SearchBase $SearchBase -Properties ProxyAddresses |
        Where-Object { $_.ProxyAddresses -like '*@Company*' } |
            Select-Object *, @{name = 'smtp'; expression = { ($_.ProxyAddresses | Where-Object { $_ -like '*@Company*' }) } }
foreach ($Contact in $ContactList) {
    foreach ($smtpAddress in $($Contact.smtp)) {
        Set-ADObject -Identity $($Contact.DistinguishedName) -Remove @{ProxyAddresses = $smtpAddress } 
    }
}

I managed to kludge together something that worked in a similar fashion:

$SearchBase = 'OU=OU,OU=OU,DC=DC,DC=dom'
$ContactList = Get-ADObject -LDAPFilter 'objectClass=Contact' -SearchBase $SearchBase -Properties ProxyAddresses |
    Where-Object { $_.ProxyAddresses -like '*@Company' } |
        Select-Object *, @{name = 'smtp'; expression = { ($_.ProxyAddresses | Where-Object { $_ -like '*@company' }) } }
foreach ($Contact in $ContactList) {
    foreach ($Search in $SearchBase){
        Set-ADObject -Identity $($Contact.DistinguishedName) -Remove @{ProxyAddresses = $($Contact.smtp) }
}}

Though it still balks at any name with a “-” in it.
It left maybe 50 out of 2500 contacts. so that many I can edit manually.
I really appreciate your help Olaf!
Have a great Xmas!!!