Mailbox database status script

For Exchange 2010.

Current functions:

  • Find database copies with failed content index
  • Fix database copies with failed content index
  • List active database copies
  • List passive database copies
  • List backup status

This script keeps returning to the main menu until exit is selected.

Use at your own risk. Don’t blame me if something goes horribly wrong.

 

The script

#Main logic
function main
{
	$menucolor = [System.ConsoleColor]::white
	write-host '-------------------------------------------------------------------'-ForegroundColor $menucolor
	write-host '|                     Mailbox database status                     |'-ForegroundColor $menucolor
	write-host '|                      Jan Kåre Lokna                          |'-ForegroundColor $menucolor
	write-host '|                     v 1.2                                       |'-ForegroundColor $menucolor
	write-host '-------------------------------------------------------------------'-ForegroundColor $menucolor
	write-host 
	write-host '** MENU **' -ForegroundColor $menucolor
	write-host '1) Find database copies with failed ContentIndex State' -ForegroundColor $menucolor
	write-host '2) Check backup status' -ForegroundColor $menucolor
	write-host '3) Find active database copies' -ForegroundColor $menucolor
	write-host '4) Find passive database copies' -ForegroundColor $menucolor
	write-host '5) Overwrite failed ContentIndex' -ForegroundColor $menucolor
	write-host '6) Exit' -ForegroundColor darkgreen
	write-host
	$menu = Read-Host "Select an option [1-6]"
	switch ($menu)
	{
		1{GetContentIndexState}
		2{GetBackupState}
		3{GetActiveCopies}
		4{GetPassiveCopies}
		5{FixContentIndex}
		6{exitMenu}
		default{. main}
	}
}



##Check backup status
function GetBackupState
{
	Get-MailboxDatabase | where {$_.Recovery -eq $False } `
		| Select-Object -Property Server, Name , LastFullBackup, LastIncrementalBackup, BackupInProgess `
		| Sort-Object -Property Server,  Name | ft -AutoSize

	Get-PublicFolderDatabase `
		| Select-Object -Property Server, Name , LastFullBackup, LastIncrementalBackup, BackupInProgess `
		| Sort-Object -Property Server, Name | ft -AutoSize
	. GoToMenu
}

##Fix contentindex
function FixContentIndex
{
	write-host 'Trying to repair failed ContentIndex:'
	$destCopy = read-host "Input destination database copy: "
	Update-MailboxDatabaseCopy $destCopy –CatalogOnly 
	. GoToMenu
}

##Find passive database copies 
function GetPassiveCopies{
	write-host 'Passive database copies:'
	try
		{
		#Get mailbox servers
		$Servers = Get-ExchangeServer | Where-Object {($_.ServerRole -match "Mailbox") }
		foreach($Server in $Servers)
		{
			Get-MailboxDatabaseCopyStatus -Server $Server.Name | Where-Object{($_.Status -ne "Mounted")}| out-default #Out-default needed to work around bug http://connect.microsoft.com/PowerShell/feedback/details/152205/bug-with-default-formatter.
		}
	}
	catch [Exception]
	{
		Write-Host "Something went horribly wrong..."
		Write-Host $_.Exception.TosSTring()
	}
	. GoToMenu
}

##Find active database copies 
function GetActiveCopies{
	write-host 'Active database copies:'
	try
		{
		#Get mailbox servers
		$Servers = Get-ExchangeServer | Where-Object {($_.ServerRole -match "Mailbox") }
		foreach($Server in $Servers)
		{
			Get-MailboxDatabaseCopyStatus -Server $Server.Name | Where-Object{($_.Status -eq "Mounted")}| out-default
		}
	}
	catch [Exception]
	{
		Write-Host "Something went horribly wrong..."
		Write-Host $_.Exception.TosSTring()
	}
	. GoToMenu
}	
	
##Find database copies with failed ContentIndex State 
function GetContentIndexState
{
	write-host 'Database copies with failed ContentIndex State:'
	try
		{
		#Get mailbox servers
		$Servers = Get-ExchangeServer | Where-Object {($_.ServerRole -match "Mailbox") }
		foreach($Server in $Servers)
		{
			Get-MailboxDatabaseCopyStatus -Server $Server.Name | Where-Object{($_.ContentIndexState -ne "Healthy")}| out-default
		}
	}
	catch [Exception]
	{
		Write-Host "Something went horribly wrong..."
		Write-Host $_.Exception.TosSTring()
	}
	. GoToMenu
}

#Exit script
function exitMenu
{
	exit
}

#Go back to menu
function GoToMenu
{
	write-host
	write-host 'Press any key to continue...' | out-default
	$s=[Console]::ReadKey($true)
	. main
}

#Start script
. main


Author: DizzyBadger

SQL Server DBA, Cluster expert, Principal Analyst

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.