Powershell 2: How to find arbitrary object in AD having a specific email address

active-directoryexchange-2010powershell

I am 3-to-4 weeks new to powershell and using it to interact with Active Directory / Exchange. (Both of which I'm also very new to.)

I've tried reading this page: http://technet.microsoft.com/en-us/library/ff730967.aspx but I'm a bit overwhelmed by all of the information, probably because I'm exhausted.

Here's what I'm trying to do. Within my code, I want to take an email address as an input string, and create a mail contact (New-MailContact) with the -ExternalEmailAddress parameter set to this string. This is trivially easy. BUT, if an object in AD already exists with this address, it won't let me create the mail contact. So, before the creation attempt, I need to determine if the call to create-mailcontact will produce an error. (The reason I need to detect this is because if this condition is occurring, I need to run a whole bunch of other commands and logic.)

What I have been trying to do is collect all 'WindowsEmailAddress' properties from the objects returned by these calls:

get-user -resultSize unlimited
get-mailcontact -resultSize unlimited

I then look in these results (I convert them to a nice in-memory hash table where the keys are the lowercase email address strings) to see if the email address I'm going to set up a contact for already exists. For most email addresses that are users in our AD domain, this works great.

Unfortunately, there are a few that don't appear in either of these lists, but I still can't create the mail contact because it says the address already exists:

The proxy address "SMTP:xxxx@xxxx.org" is already being used by
"our.domain.org/Exchange Contacts/Some User Name". Please choose another proxy
address.
  + CategoryInfo          : NotSpecified: (our.domai...xx@xxxx.org:ADObjectId) [New-MailContact], ProxyAddressExistsException
  + FullyQualifiedErrorId : 6580586A,Microsoft.Exchange.Management.Recipient Tasks.NewMailContact

I am a bit afraid to iterate through the list returned by get-recipient… is that what I need to do? I was thinking there is probably an easy way to accomplish this I haven't figured out yet.

Important note: There are about 20-30 addresses, all returning this type of error. HOWEVER, when you look above and see 'Exchange Contacts' in the error, well, that portion of the string is completely different from one faulty email address to the next. Not sure if that's relevant.
Please feel free to set me straight on anything I'm not understanding. Thanks!

Best Answer

Note that the "mail" attribute is not the only place where an email address may be located. There is also a multi-valued attribute "proxyAddresses", where additional email addresses may be assigned to an object.

Someone has created a script here: (requires the free Quest AD cmdlet)

http://poshcode.org/1594

    Param (
    [Parameter(Mandatory=$true,
        Position=0,
        ValueFromPipeline=$true,
        HelpMessage="Enter SMTP address to search for in Active-Directory."
    )]
    [string]$objSMTP
    )
Function Get-ProxyAddresses ([string]$Address){
$objAD = $null
$objAD = Get-QADObject -LdapFilter "(proxyAddresses=*$Address*)" -IncludeAllProperties -SizeLimit 0 -ErrorAction SilentlyContinue
Write-Output $objAD
}#Close Function
#Validate Quest PSSnapin is loaded
Add-PSSnapin -Name Quest.ActiveRoles.ADManagement -ErrorAction SilentlyContinue
#Run Function to search AD for SMTP address
$Results = $null
$Results = Get-ProxyAddresses -Address $objSMTP | Select-Object Name,DisplayName,ObjectClass,Email,AccountisDisabled,AccountisLockedOut,MailNickName,LegacyExchangeDN -ErrorAction SilentlyContinue
IF($Results -eq $null){
Write-Host ""
Write-Host "No Object Found with .attribute[proxyAddress] containing $objSMTP."}
Else{$Results | Format-List *}
#End