Thursday, April 5, 2018

Exchange newest mailbox redistribution script (move-MailboxesMostRecent.ps1)

4/5/2018: This script leverages the Exchange-2010+ 'Automatic Mailbox Provisioning' (AMP) feature, wherein, if you don't designate a specific destination database for a mailbox move, AMP will pick one at random for you (following some predictable rules, covered below).

When moving mailboxes, to redistribute load, the most 'easy-to-move' of the bunch, are boxes that are brand new, or those only in use for a very limited amount of time.

Using this simple script you specify a target database you want to redistribute mailboxes from (via the -database parameter), and the number of 'most recently created' mailboxes you want redistributed (via the -number parameter).

From there, AMP pulls all mailboxes in the specified database, sorts them on whenCreated property, and initiates moves (suspended by default) for each of the targeted mailboxes, letting AMP determine new databases to host the boxes.

Something like so...

.\move-MailboxesMostRecent.ps1 -Database db1 -Number 100 -whatif ;

... , to move the most recent 100 mailboxes in the 'db1' database (with the -whatif testing parameter in this case).

AMP is well documented by Microsoft here: Automatic mailbox distribution, but at a minimum, prior to using this script, you'll want to 'mark' any 'congested' dbs to be distributed away-from, from Automatic Mailbox Provisioning (AMP) by using this command mark them excluded from AMP, one by one:

Set-MailboxDatabase -Identity dbID -IsExcludedFromProvisioning $true ;   

You can also do the usual pipeline things, like get a mail server (with get-exchangeserver et al) and pipe it into get-mailboxdatabase and then into the above, to 'Exclude' all databases on a given server.

The MS AMP article linked above covers the subject in great detail, but here is the process tree that AMP walks to select destationation databases (for both new mailboxes and moved mailboxes):

  1. Exchange retrieves a list of all mailbox databases in the Exchange 2013 organization.
  2. Any mailbox database that's marked for exclusion from the distribution process is removed from the available list of databases. You can control which databases are excluded. For more information, see Exclude Databases from Automatic Distribution later in this topic.
  3. Any mailbox database that's outside of the database management scopes applied to the administrator performing the operation is removed from the list of available databases. For more information, see Database Scopes later in this topic.
  4. Any mailbox database that's outside of the local Active Directory site where the operation is being performed is removed from the list of available databases.
  5. From the remaining list of mailbox databases, Exchange chooses a database randomly. If the database is online and healthy, the database is used by Exchange. If it's offline or not healthy, another database is chosen at random. If no online or healthy databases are found, the operation fails with an error.

Pretty simple, but handy for quickly initiating re-balance moves. Full instructions contained within the comment help in the script.

Here's a gist with the key working pieces of code

No comments:

Post a Comment