########################################################################################
#Date:25 Oct 2010
#Script:Checkexhealth.ps1
#Description:Check services on Exchange servers and check Queue status of transport servers
#Sends out an email if it finds some issue
#
#
#
########################################################################################
$probfound=""
Function docheck ($a)
{
clear
Write-host ""
Write-host "Server Name:" $a.name -ForegroundColor Yellow
Write-host "Server Role:" $a.ServerRole -ForegroundColor Green
Write-Host "Running service health Check"
$o1=Test-ServiceHealth $a.name|where {$_.requiredservicesrunning -eq $false}
if ($o1 -ne $null)
{
$script:probfound += "<TR bgcolor=cyan><TD><p align=center> Problem Found with services on <b>" + $a.Name + " </b> with these server roles <i>" + $a.serverrole + ".</i> </TD></TR>"
$script:probfound += "<TR><TD><p align=center><font size=2 color=red ><i>" + $($o1.servicesnotrunning ) + " </i> </font>is not running.</TD><TR>"
}
if ($a.IsHubTransportServer -eq $true)
{
Write-Host "Getting Queue (Queues in Retry state) Info.."
$o2=Get-Queue -Server $a.Name -filter {status -eq "retry"} -ResultSize unlimited
if ($o2 -ne $null)
{
$script:probfound+= "<TR bgcolor=cyan><TD><p align=center>" + "Problem Found with <b>" + $a.Name + " </b> with these Queues in Retry State. </TD></TR>"
foreach ($q in $o2)
{
$script:probfound+= "<TR><TD><p align=center>" + $($q.identity.toString()) + " with next hop domain " + $($q.nexthopdomain.tostring()) + "<font size =2 color=red> (" + $q.messagecount + ")</font><br></TD></TR>"
}
}
}
}
Function emailsend
{
$probfoundhead="<HTML><BODY><TABLE align=center cellspacing=0 bordercolor=black border=1>"
$emailFrom = "Mail From field"
$emailTo = "Email Address To field"
$subject = "Exchange servers status"
$script:probfound+="</Table></Body></HTML>"
$body = $probfoundhead + $script:probfound
$smtpServer = "SMTP server name"
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg = New-Object Net.Mail.MailMessage($emailFrom, $emailTo, $subject, $body.ToString())
$msg.IsBodyHTML = $true
$smtp.Send($msg)
}
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin
if ($($args.count) -ne 0)
{
$standalonever=$true
foreach ($svr in $args)
{
docheck (get-ExchangeServer $svr)
}
}
else
{
foreach ($a2 in (get-exchangeserver))
{
docheck $a2
}
}
if ($probfound.Length -ne 0)
{
if ($standalonever -ne $true)
{
emailsend
}
else
{
$probhtml="<HTML><BODY><TABLE align=center cellspacing=0 bordercolor=black border=1>" + $probfound + "</Table></Body></HTML>"
$probhtml|out-file exchangeservicehealth.htm
invoke-expression .\exchangeservicehealth.htm
}
}
else
{
Write-Host "All is well :)"
}