#<.#Now, let’s put everything together and tell the script that if something is wrong with any database to send an e-mail to the administrator. This way we can create a schedule task to run this script every 2 minutes, for example, send an e-mail in case of any issue and stop the scheduled task (otherwise you will receive e-mails every 2 minutes until everything is back to normal – which is also an option). .#>

FunctionGet-ExchangeServerADSite ([String] $excServer)

{

       # We could use WMI to check for the domain, but I think this method is better

       # Get-WmiObject Win32_NTDomain -ComputerName $excServer

 

       $configNC=([ADSI]"LDAP://RootDse").configurationNamingContext

       $search=new-objectDirectoryServices.DirectorySearcher([ADSI]"LDAP://$configNC")

       $search.Filter ="(&(objectClass=msExchExchangeServer)(name=$excServer))"

       $search.PageSize = 1000

       [Void] $search.PropertiesToLoad.Add("msExchServerSite")

 

       Try {

              $adSite= [String] ($search.FindOne()).Properties.Item("msExchServerSite")

              Return ($adSite.Split(",")[0]).Substring(3)

       } Catch {

              Return$null

       }

}

 

 

[Bool] $bolFailover=$False

[String] $errMessage=$null

 

# Check if all databases are currently mounted on the server with ActivationPreference of 1

Get-MailboxDatabase | Where {$_.Recovery -eq$False} | SortName | ForEach {

       $db=$_.Name

       $curServer=$_.Server.Name

       $ownServer=$_.ActivationPreference | ? {$_.Value -eq 1}

      

       # Compare the server where the DB is currently active to the server where it should be

       If ($curServer-ne$ownServer.Key.Name) {

              # Get the AD sites of both servers

              $siteCur=Get-ExchangeServerADSite$curServer

              $siteOwn=Get-ExchangeServerADSite$ownServer.Key

             

              # Check if both servers are on different AD sites

              If ($siteCur-ne$null-and$siteOwn-ne$null-and$siteCur-ne$siteOwn) {

                     $errMessage+="`n$db on $curServer should be on $($ownServer.Key) (DIFFERENT AD SITE: $siteCur)!"  

              } Else {

                     $errMessage+="`n$db on $curServer should be on $($ownServer.Key)!"

              }

 

              $bolFailover=$True

       }

}

 

$errMessage+="`n`n"

 

# Check the Status of all databases including Content Index and Queues

Get-MailboxDatabase | Where {$_.Recovery -eq$False} | SortName | Get-MailboxDatabaseCopyStatus | ForEach {

       If ($_.Status -notmatch"Mounted"-and$_.Status -notmatch"Healthy"-or$_.ContentIndexState -notmatch"Healthy"-or$_.CopyQueueLength -ge 200 -or$_.ReplayQueueLength -ge 200) {

              $errMessage+="`n`n$($_.Name) - Status: $($_.Status) - Copy QL: $($_.CopyQueueLength) - Replay QL: $($_.ReplayQueueLength) - Index: $($_.ContentIndexState)"

              $bolFailover=$True

       }

}

 

If ($bolFailover) {

       # Disable the schedule task that runs the script and send e-mail to administrator

       Schtasks.exe /Change /TN "MonitorDAG" /DISABLE

       Send-MailMessage-From"exchange@letsexchange.com"-To"admin@letsexchange.com"-Subject"DAG NOT Healthy!"-Body$errMessage-PriorityHigh-SMTPserver"smtp.letsexchange.com"-DeliveryNotificationOptiononFailure

}