Exchange 2010 – Script to find unhealthy content indexes

2017-12-07T09:33:15+00:00 April 2nd, 2012|Exchange, PowerShell|

I was asked by an Exchange Administrator who was new to Exchange 2010 to see if I could set up a simple script that would check the state of the Exchange databases that may affect it failing over in the event of an outage.

The following script simply checks the state of each database copy and the state of the context index. This can be copied and pasted into a text file and saved as a PS1 file and run in an Exchange Management PowerShell window:

###############################################################

write-host "Mailbox Databases that are not healthy or mounted" -foregroundcolor yellow

$mounted = get-mailboxdatabase | Get-MailboxDatabaseCopyStatus | Where-Object {$_.Status -ne "Healthy"}
$failed = $mounted | Where-Object {$_.Status -ne "Mounted"}

$failed | ForEach-Object {$db_count++}

$db_total = $db_count -1

If ($db_total -eq 0)
{
Write-Host `n
write-host "All database states are ok" -foregroundcolor green
Write-Host `n
}
ElseIf ($db_total -gt 0)
{
Write-Host `n
write-host "Databases need reseeding, mounting or resuming replication" -foregroundcolor red
$failed
Write-Host `n
}

Write-Host `n
Write-Host `n

write-host "Mailbox Databases that have unhealthy content indexes" -foregroundcolor yellow

$indexes = get-mailboxdatabase | Get-MailboxDatabaseCopyStatus | Where-Object {$_.ContentIndexState -ne "Healthy"}

$indexes | ForEach-Object {$index_count++}

$index_total = $index_count -1

If ($index_total -eq 0)
{
Write-Host `n
write-host "All database indexes are ok" -foregroundcolor green
Write-Host `n
}
ElseIf ($index_total -gt 0)
{
Write-Host `n
write-host "Database context indexes are failed" -foregroundcolor red
$indexes
Write-Host `n
}

###############################################################

The script will simply output like this if there is no problems:

image

If there are any problems, it will list out the indexes or databases that are not reporting as they should be and you can take the corrective actions accordingly.

Cheers

Paul