How to use PowerShell to set-mailbox alias Exchange 2010

2k views Asked by At

I have an odd request I'm struggling with in PowerShell. We have a default email policy that names new user email addresses first initial last [email protected], which is fine for the first requirement.

After that they want the following, and I can't figure out how to code/test against what exists in Exchange? Should I do the test from AD first since it might hold the SMTP address and make adjustments if a duplicate alias exists? We have over 20k users so duplicates happen a lot.

Also, I've tested this against the Exchange 2010 Management Console successfully to Set-Mailbox -identity s1112223 -Alias $SamAccountName ("{0}.{1}" -f $_.givenname, $_.sn) which gives me [email protected], but how do I format based on the criteria below?

  1. First Initial, Last name (if already taken go to the next convention)
  2. First initial, middle initial, last name (if already taken or no middle initial provided, go to the next convention)
  3. Preferred first name, Last name (if already taken or if no preferred first name, go to the next convention)
  4. Full first name, full last name (if already taken, go to the next convention)
  5. Full first name, middle initial, last name (if already taken, go to the next convention)
  6. First two letters of the first name, last name (if already taken, use first three letters of first nameā€¦.and so on until it is unique. If all variations are taken, go to the next convention)
  7. First initial, last name and a number (THIS IS NOT PREFERRED, but the absolute last resort)
1

There are 1 answers

0
DevinGanger On

Typically, you build one or more email address policies that apply the desired naming critera, and they will automatically increment if there's a collision with an existing object when provisioning a new object. If you're trying to pre-query the existing aliases/email addresses to avoid those numbered duplicates, then you have to keep track of what has been already issued.

The easiest way is to track the .EmailAddresses field of all of your Exchange recipients -- all Mailboxes, MailUsers, MailContact, mail-enabled public folders, distribution groups, etc. This field holds all of the proxy addresses a given object has been configured with. However, running those queries will take a lot of time, so your program will be slow.

I would suggest that you use a scheduled task to query those and place them into a database. Then, your provisioning code can (a) do a simple query against the database to see if a given alias or the resulting email address is already taken, and (b) update the database with the new alias and email addresses after a new object is successfully provisioned.

Trying to do this in PowerShell as you're provisioning the mailbox might be the wrong place to do it. You may need to have this unique string generated from the same source that is generating all of the rest of the user data to begin with, and then PowerShell can simply use that to set the alias of the created object.