Exchange Server | Murat Bilici

"Empower your knowledge"

Monitor Databases in DAGs

Function sendEmail ([String] $body)
{
	$MailMessage = New-Object System.Net.Mail.MailMessage
	$MailMessage.From = "exchange@letsexchange.com"
	$MailMessage.To.Add("nuno@letsexchange.com")
	$MailMessage.Subject = "DAG Not Healthy!"
	$MailMessage.Body = $body
	$MailMessage.Priority = "High"

	$SMTPClient = New-Object System.Net.Mail.SMTPClient
	$SMTPClient.Host = "HTCAS1.letsexchange.com"
	$SMTPClient.Send($MailMessage)
}

Function getExchangeServerADSite ([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-object DirectoryServices.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

Get-MailboxDatabase | Sort Name | 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)
	{
		# Compare the AD sites of both servers
		$siteCur = getExchangeServerADSite $curServer
		$siteOwn = getExchangeServerADSite $ownServer.Key

		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"

Get-MailboxServer | Get-MailboxDatabaseCopyStatus | ForEach {
	If ($_.Status -notmatch "Mounted" -and $_.Status -notmatch "Healthy" -or $_.ContentIndexState -notmatch "Healthy")
	{
		$errMessage += "`n$($_.Name) - Status: $($_.Status) - Index: $($_.ContentIndexState)"
		$bolFailover = $True
	}
}

If ($bolFailover)
{
	sendEmail $errMessage
	#Schtasks.exe /Delete /TN "MonitorDAG" /F
}

# # MonitorDAG.ps1 # Script to monitor DAG in Exchange 2010 # # Nuno Mota # Team @MSExchangeGuru
Categories: Exchange 2010 - Scripts