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

#defined variables

$sendemailalertto="exchangeteam@companyx.com"

$YourSMTPServer="exchange.fqdn.com"

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

#Define email for DAG notifciation

function send_DAG_email {

$EmailFrom = "exchange_server@companyx.com"

$EmailTo = $sendemailalertto

$EmailSubject = "Exchange Replication Is Unhealthy"

$SMTPServer = $YourSMTPServer

$emailbody = "Please see attached"

$emailattachment = "c:\DatabaseCopyStatus.csv"

$mailmessage = New-Object system.net.mail.mailmessage

$mailmessage.from = ($emailfrom)

$mailmessage.To.add($emailto)

$mailmessage.Subject = $emailsubject

$mailmessage.Body = $emailbody

$attachment = New-Object System.Net.Mail.Attachment($emailattachment, 'text/plain')

$mailmessage.Attachments.Add($attachment)

$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)

$SMTPClient.Send($mailmessage)

}

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

#Define email for INDEX notification

function send_INDEX_email {

$EmailFrom = "exchange_server@companyx.com"

$EmailTo = $sendemailalertto

$EmailSubject = "Exchange Replication Failed"

$SMTPServer = $YourSMTPServer

$emailbody = "Please take action. Database Content Index is not healthy `n

Run below cmdlet to repair the index database.`n

Update-MailboxDatabaseCopy $mailboxdatabase -CatalogOnly`n

`n

For more information, see:`n

http://technet.microsoft.com/en-us/library/dd335201.aspx

"

$emailattachment = "c:\Bad_ContentIndexState_DBs.csv"

$mailmessage = New-Object system.net.mail.mailmessage

$mailmessage.from = ($emailfrom)

$mailmessage.To.add($emailto)

$mailmessage.Subject = $emailsubject

$mailmessage.Body = $emailbody

$attachment = New-Object System.Net.Mail.Attachment($emailattachment, 'text/plain')

$mailmessage.Attachments.Add($attachment)

$mailmessage.IsBodyHTML = $true

$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)

$SMTPClient.Send($mailmessage)

}

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

# Import Exchange related powershell modules

# Connect to Exchange RBAC session

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

$s = New-PSSession -ConfigurationName Microsoft.Exchange.Management.PowerShell.2010 -ConnectionUri http://exchange.fqdn.com/powershell

Import-PSSession -Session $s

sleep -seconds 10

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

# get-mailboxdatabase and copystatus:

# these appear to return objects.

# each object has multiple fields we are interested in

# specifically we are comparing "Status'

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

$alert1=0

$alert2=0

# Array to store db entries that are not healthy or mounted

$error1=@()

$error2=@()

# get the db of mailbox info

Get-mailboxdatabase | Get-MailboxDatabaseCopyStatus | foreach {

# Cycle through db entries, one line at a time

# If status is _not_ healthy

if ($_.Status -ne "Healthy") {

# If status is _not_ mounted

if ($_.Status -ne "Mounted") {

# If status is _not_ disconnectedandhealthy

if ($_.Status -ne "DisconnectedAndHealthy") {

# We have an alert condition

$alert1=1

# Store alerting device details

$error1+=[Array] $_

}

}

}

# If index is not healthy

if ($_.contentindexstate -ne "Healthy"){

# set 2nd alert flag to 1

$alert2=1

# add the current entry to your error2 array

$error2+[Array] $_

}

}

# if status alert on DAG health, write results to csv, then send the alert email.

if ($alert1 -ne 0) {

$error1 | select Name,DatabaseName,Status,MailboxServer,ActiveDatabaseCopy,ActivationSuspended | export-csv -Path c:\DatabaseCopyStatus.csv -NoTypeInformation

send_DAG_email

Write-Host "DatabaseFailure"

}

# if status alert on INDEX health, write results to csv, then send the alert email.

if ($alert2 -ne 0) {

$error2 | select Name,DatabaseName,Status,MailboxServer,ActiveDatabaseCopy,ActivationSuspended | export-csv -Path c:\Bad_ContentIndexState_DBs.csv -NoTypeInformation

send_INDEX_email

Write-Host "IndexFailure"

}

# for testing purposes, created a condition to test monitoring integration.

# else {

# Write-Host "TESTFailure"