Tuesday, October 21, 2008

Powershell - Get status of Exchange databases

The Exchange Management Shell (EMS) provides a way to output the status of Exchange Databases through the command, get-mailboxdatabase. Note that you must include the -status switch in order to get the proper output.
get-mailboxdatabase select Mounted - will give you nothing.
The correct command is
get-mailboxdatabase -status Select Name, Mounted, LastFullBackup

I have a small script I wrote that get's the status of all the databases in my organization and sends me an e-mail if a database is dismounted. I have the script running as a scheduled task. Here it is:
**Save this a a .ps1 file ** It'll need to be signed as well

function Send-Mail
$SmtpClient = new-object system.net.mail.smtpClient
$MailMessage = New-Object system.net.mail.mailmessage
$SmtpClient.Host = 'relayserver'
$mailmessage.from = 'fromme@company.com'
$mailmessage.Subject = $sbj
$mailmessage.IsBodyHtml = 1
$mailmessage.Body = $msg
$mailmessage.Body = $msg

function exch-status {
get-mailboxdatabase -status %{$DBName = $_.Name; $DBMounted = $_.Mounted; $DBBackup = $_.LastFullBackup}

if ($DBMounted -eq $False )
$Message = "The database $DBName is unmounted. Please page Sys Admin immediately."
Send-Mail 'Exchange DB Unmounted' $Message 'copmpanyops@company.com'
$DateToday = Get-Date
if($DBBackup.day -lt $DateToday.day)
Message = "It's been 24 hours since a full backup completed successfully."
Send-Mail 'Full Backup has not run' $Message 'copmpanyops@company.com'

****End of Script ****