Quick PowerShell Stuff 15

In this next quick PowerShell post, I am going to cover a quick way to look at your database copies. This script will look for failed copies of the database. I wrote this because of a client I have that have over 150 databases and over 500 copies of all of their databases. This script provides a simple view into this part of your Exchange databases.

The Script

The code for the script is listed below. Fairly simply, the script will make sure no copies are failed. If failed copies are found then it will process lines 24-47. At this point all the script will determine how many are good and bad, and provide a total copy list as well. If all copies are good, then the script will process lines 10-22.

Cls
# Setting up counters for later.
$m=0
$h=0
$u=0

write-host "Checking database copies for ones that are in a 'failed' state......." -foregroundcolor Yellow

$check = Get-MailboxDatabase | Get-MailboxDatabaseCopyStatus | where {($_.status -ne "mounted") -and ($_.status -ne "healthy")}
if ($check -eq $null) {
    write-host " "; write-host "All database copies are 'healthy' or 'mounted'.  " -ForegroundColor green -nonewline
    write-host "There is no need for remediation." -ForegroundColor white ;write-host " "
    $check2 = Get-MailboxDatabase | Get-MailboxDatabaseCopyStatus
    foreach ($line in $check2) {
        $status = $line.status
        if ($status -eq "mounted") {$m++}
        if ($status -eq "healthy") {$h++}
    }
    write-host "Mounted database copies - " -ForegroundColor Cyan -nonewline;write-host "$m" -ForegroundColor white
    write-host "Healthy database copies - " -ForegroundColor Cyan -nonewline;write-host "$h" -ForegroundColor white
    $total = $h+$m+$u
    write-host "------------------------------"
    write-host "Total database copies   - " -ForegroundColor Cyan -nonewline;write-host "$total" -foregroundcolor white;write-host " "
} else {
        write-host " ";write-host "These database copies were found to be in an unhealthy state:" -ForegroundColor cyan;write-host " "
        foreach ($line in $check) {
            $name = $line.name
            $status = $line.status
            write-host "The database copy " -ForegroundColor white -nonewline
            write-host "$name " -ForegroundColor red -nonewline
            write-host "is in a " -ForegroundColor white -nonewline
            write-host "$status" -ForegroundColor Red -nonewline
            write-host " state.  Please remediate this as soon as possible." -ForegroundColor yellow
	    $u++
        }
        write-host " ";write-host "Verifying how many copies are healthy" -ForegroundColor yellow;write-host " "
        $check2 = Get-MailboxDatabase | Get-MailboxDatabaseCopyStatus
        foreach ($line in $check2) {
            $status = $line.status
            if ($status -eq "mounted") {$m++}
            if ($status -eq "healthy") {$h++}
        }
        write-host "Unhealthy database copies - " -ForegroundColor Red -nonewline;write-host "$u" -ForegroundColor white
        write-host "Mounted database copies - " -ForegroundColor Cyan -nonewline;write-host "$m" -ForegroundColor white
        write-host "Healthy database copies - " -ForegroundColor Cyan -nonewline;write-host "$h" -ForegroundColor white
        $total = $h+$m+$u
        write-host "------------------------------"
        write-host "Total database copies   - " -ForegroundColor Cyan -nonewline;write-host "$total" -foregroundcolor white;write-host " "
}

Script Run – Unhealthy Copies

In the below image, we have an instance where there were some failed database copies found in the Exchange environment. We see that of 9 total copies, 3 were bad, 2 were mounted and 3 were in a healthy state. Note that we also have a list of the database copies that are unhealthy or even dismounted.

Copies-FailedDetected

Script Run – All Healthy Copies

Now in the case (which I hope is 99.9% of the time!) that all database copies are all healthy, then this is what you will see after the script is run:

Copies-NoFailedDetected

Similar to the unhealthy run through, we will see the number of copies that are healthy and the number of databases that are mounted.

With some modification this script could send this in an email as an ‘alert’ or informational message. This would especially be useful in a situation where there are updates being processed or if there are known issues in the environment.

Advertisements

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