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:firstname.lastname@example.org" is already being used by "our.domain.org/Exchange Contacts/Some User Name". Please choose another proxy address. + CategoryInfo : NotSpecified: (our.domai...email@example.com: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!