Quick PowerShell Stuff 13

After a long hiatus, I will be writing a few more quick PowerShell articles that I hope will prove helpful to my readers. For this 13th installment, I am going to cover how to quickly check your databases (in a DAG) to make sure the database copy that is mounted is has an activation preference of 1. I wrote this script for a client who wished to have their databases ‘just right’. Let’s do a quick walk-through of the script:

The Script

I wrote this script for a company with 8 sites and 8 different DAGs. Their DAGS are site specific with 2 servers in the primary physical site (like Miami) and 2 more servers in a DR site (Like Minneapolis). This script will look at each database by DAG and check to see if the mounted copy has an Activation Preference of 1.

$dag = Get-DatabaseAvailabilityGroup
foreach ($line in $dag) {
    $dagname = $line.name
    write-host "Examining the Database Availability Group $dagname" -ForegroundColor Yellow
    # Get each database in each DAG
    $ap = (Get-MailboxDatabase | where {$_.MasterServerOrAvailabilityGroup -eq $dagname})

    # Loop for each DAG
    foreach ($line in $ap) {
        $server = $line.server
        $database = $line.name
        $set = $line.ActivationPreference
        foreach ($line2 in $set) {
            # normalize variables for later use
            $value = $line2.value
            $server = $line2.key

            # Find the database copy with an Activation Preference of 1
            if ($line2.value -eq "1") {
                write "Server $server with database $database has the activation preference of $value"
                # Find the mounted copy of the database
                $mounted = get-mailboxdatabase $database
                $currentserver = $mounted.server.name
                # Check to see if the copy mounted has an Activation Preference of 1
                if ($server -ne $currentserver) {
                    write-host "The server " -nonewline -foregroundcolor Cyan
                    write-host $currentserver -nonewline -foregroundcolor Red
                    write-host " is not the correct server.  Please move the database to " -nonewline -foregroundcolor Cyan
                    write-host $server -nonewline -foregroundcolor Red
                    write-host " "
    write-host " "

For the same client, I ran the script as a check after they had performed some maintenance over a patch weekend. Here is what we saw:


Notice the red color in the results. These show that the above database is mounted on the wrong server. Listed in the line is where the DB is now and what server should be hosting it.

To fix the balance of these databases, you would run the ‘RedistributeActiveDatabases.ps1’ script that is in the Scripts directory on your Exchange server. The entire command would look something like this:

.\'RedistributeActiveDatabases.ps1 -DagName DAG01-S03 -BalanceDbsByActivationPreference -ShowFinalDatabaseDistribution -Confirm:$false

This puts our databases back the way we want and if I were to run the check again, I would see no more red:


That’s it. Easy way to troubleshoot and fix any database imbalance in your Exchange environment.

** Please note –> This has only been tested in an Exchange 2010 SP3+ environment so far


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s