Exchange 2010 /2007 monitoring script
This powershell script monitor virous services on domain controllers, Exchange services, Exchange databases and performs a mail flow test.
1. Open notepad
2. Copy and paste below text to notepad
3. Save the file with .ps1 extension.
# ---------- SCRIPT STARTS HERE--------------
$runscript="true"
Do {
function SendEmail
{param ($title,$body)
$SmtpServer = "mail.domain.com"
$From = "it@domain.com"
$To = "administrator@domain.com"
$SmtpClient = New-Object system.net.mail.smtpClient
$SmtpClient.host = $SmtpServer
$SmtpClient.Send($From,$To,$Title,$Body)
}
#CSV spreadsheet must contain hostname and role columns
#multiple roles - dc,hubcas,mailbox,
#28. Create monitoring script for major services and forward eventlongs via WMI?
#> services, processor, forward error eventlogs(wmi)?, exchange test commandlets
#> monitor cluster resources / if DBs are mounted (check if msexchangeis is start, if yes.. check dbs are mounted)
#forward error event logs
# For DCs, it monitors DNS, DHCP, DFS, NTFRS, KDC, NETLOGON, W32TIME
$serverlist="c:\AD_exchange_serverlist.csv"
import-CSV $serverlist | foreach-object {
if ( $_.role -match "dc" ) {
$hostname=$_.hostname
$dnsservice=gwmi -computername $hostname -query "select * from win32_service where name='dns'"
if ($dnsservice.state -eq "Stopped") {SendEmail -title "$hostname - DNS service is stopped" -body "$hostname DNS service is stopped, please take action"}
$dhcpservice=gwmi -computername $hostname -query "select * from win32_service where name='DHCPServer'"
if ($dhcpservice.state -eq "Stopped") {SendEmail -title "$hostname - DHCP service is stopped" -body "DHCP service is stopped on $hostname, please take action"}
$dfsservice=gwmi -computername $hostname -query "select * from win32_service where name='DFS'"
if ($dfsservice.state -eq "Stopped") {SendEmail -title "$hostname - DFS service is stopped" -body "DFS service is Stopped on $hostname, please take action"}
$ntfrsservice=gwmi -computername $hostname -query "select * from win32_service where name='NtFrs'"
if ($ntfrsservice.state -eq "Stopped") {SendEmail -title "$hostname - NtFRS service is stopped" -body "NtFRS service is stopped on $hostname, please take action"}
$kdcservice=gwmi -computername $hostname -query "select * from win32_service where name='kdc'"
if ($kdcservice.state -eq "Stopped") {SendEmail -title "$hostname - KDC service is stopped" -body "KDC service is stopped on $hostname, please take action"}
$netlogonservice=gwmi -computername $hostname -query "select * from win32_service where name='netlogon'"
if ($netlogonservice.state -eq "Stopped") {SendEmail -title "$hostname - NetLogon service is stopped" -body "NetLogon service is stopped on $hostname, please take action"}
$timeservice=gwmi -computername $hostname -query "select * from win32_service where name='w32time'"
if ($timeservice.state -eq "Stopped") {SendEmail -title "$hostname - W32Time service is stopped" -body "W32Time service is stopped on $hostname, please take action"}
}
if ( $_.role -match "hubcas" ) {
$hostname=$_.hostname
$stoppedservices=gwmi -computername $hostname -query "select * from win32_service where state='stopped' and startmode='auto'"
$servicestatus=Test-ServiceHealth -Server $hostname
$servicestatus | foreach { if ($_.requiredservicesrunning -ne $true){sendemail -title "$hostname - Some services are not running" -body $stoppedservices}}
}
if ( $_.role -match "mailbox" ) {
$hostname=$_.hostname
$stoppedservices=gwmi -computername $hostname -query "select * from win32_service where state='stopped' and startmode='auto'"
$servicestatus=Test-ServiceHealth -Server $hostname
$servicestatus | foreach { if ($_.requiredservicesrunning -ne $true){sendemail -title "$hostname - some services are not running" -body $stoppedservices}}
$mailflowtest=Test-Mailflow -Identity $hostname
if ($mailflowtest.testmailflowresult -ne "Success") {sendemail -title "$hostname - Mailflow test Failed" -body $mailflowtest}
$dbstatus=Get-MailboxDatabase -Status -server $hostname
$dbcheck=Get-MailboxDatabase -Status -server $hostname | foreach { if ($_.mounted -ne $true) {sendemail "Database disounted: $_" "$_ on $hostname is dismounted. Please take action."}}
}
}
sleep -Seconds 3600
} while ($runscript -eq "true")
# ---------- SCRIPT ENDSS HERE--------------