Quick PowerShell Stuff 14

In this quick PowerShell post I will cover a quick way to get a handle on corrupt content indexes for your databases. In Exchange the content index is created by the Exchange Search processes for each database in your DAG. When the index is corrupt it is possible that clients will suffer delays or not be able to search at all via their clients. In order to prevent this from becoming an issue, I wrote a script that I can run during the day (or night) to check the status of all content indexes for all mailbox databases in Exchange.

The Script

This script was written with a specific purpose in mind, find the corrupt indexes, display which ones were corrupt and offer an option to fix the indexes (or an option to not fix them as well). The script will go through each database copy to see if they content index state is not ‘healty’. This allows the script to look for any index that is in an unstable state.

cls
write-host "Checking database content indexes now." -foregroundcolor Yellow
# $test =(Get-MailboxDatabase | Get-MailboxDatabasecopystatus | where {$_.contentindexstate -ne "healthy"})

if ($test -eq $null) {
    write-host " "
    write-host "No databases have failed content indexes!" -foregroundcolor cyan;write-host " "
} else {
    write-host " "
    foreach ($line in $test) {
        $name = $line.name
        write-host "The database " -nonewline
        write-host "$name" -foregroundcolor green -nonewline
        write-host " has a " -nonewline
        write-host "failed content index" -foregroundcolor red -nonewline
        write-host ".  Make sure to fix this."
    }
    write-host " "
}

write-host "Do you want to repair the failed content indexes? [y or n]" -ForegroundColor Cyan -nonewline
$answer = read-host
if ($answer -eq "y") {
    foreach ($line in $test) {
        $identity = $line.name
        # Update-MailboxDatabaseCopy -identity $identity -CatalogOnly
        # get-mailboxdatabase -identity $identity
    } 
}else {
        write-host " " 
        write-host "Please make sure to repair your failed content indexes soon."  -ForegroundColor yellow
        write-host " " 
}

Scenario One

The script can be run just as a check to verify the content indexes are in good shape. If a content index is found to be corrupt it will appear in the list as show below. Now, if you do not want to fix the indexes for change control reasons or whatever, then the script allows for an exit point to not update the indexes. The script will also remind you to fix the issues:

FailedIndex-NoCorrect

Scenario Two

Now if content indexes come up as failed and you can fix them automatically, the script, as show below, will allow for this to be done and report which databases it is working on:

FailedIndex-Correcting

Content Indexes – All Healthy
If the script is run either after the indexes were fixed or when they were already healthy, the script would generate this for output:

AllIndexesClean

Further Reading

Issue this script will not fix
Old article on Content Indexing (27)
Exchange 2013 article on Exchange Search
Exchange Search articles on Technet

Advertisements

One thought on “Quick PowerShell Stuff 14

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