1. Create a serverstatus.csv file like the following, to list all the SMTP servers that you need to monitor.
#TYPE System.Management.Automation.PSCustomObject
"Name","IP","LastStatus","CurrentStatus","Updatetime"
"MKxxMX01","208.x.x.x","SUCCESS","SUCCESS","4/16/2012 10:24 AM"
"MKxxMX02","71.x.x.x","SUCCESS","SUCCESS","4/16/2012 10:24 AM"
"MKxxMAL03","192.168.10.x","SUCCESS","SUCCESS","4/16/2012 10:24 AM"
"MKxxMAL04","192.168.10.x","SUCCESS","SUCCESS","4/16/2012 10:24 AM"
"MKxxMAL01","192.168.40.x","SUCCESS","SUCCESS","4/16/2012 10:24 AM"
2.Create a count.txt. Type 0 inside the file. If it gets errors, it sends our alert, very 15 minutes. If it keeps getting more than 4 errors, it will send out the alert every 1 hour.
0
3.Create InfoRecipients.txt. List the mail address who will receive the alter.
6264x63xxx@tmomail.net,ts@xxxxxxxx.com,xxxx.techsupport@gmail.com
4.PowerShell Script.
##########################################################
## Send mail to test the mail server!! ##
##########################################################
$ErrorActionPreference = "silentlycontinue"
#$ErrorActionPreference = "continue"
[Int]$Count= Get-Content -Path count.txt
$CurrentStatus = "SUCCESS"
$Serverlist = Import-Csv serverstatus.csv
$emailFrom = "SMTPtest@xxxamericaxx.com"
$emailTo = Get-Content -Path testaddress.txt
$subject = "Mail test from Chicago every 15 minutes"
$body = "Mail test from Chicago DataCenter."
foreach ( $Server in $Serverlist)
{
$Server.LASTSTATUS=$Server.CURRENTSTATUS
$smtpServer= $Server.IP
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom, $emailTo, $subject, $body)
$TestStatus=$?
if ($TestStatus)
{
$Server.CurrentStatus= "SUCCESS"
}
ELSE
{
$Server.CURRENTSTATUS= "FAILURE"
$CurrentStatus = "FAILURE"
}
$Server.UPDATETIME = Get-Date -Format g
}
$Serverlist | Export-Csv serverstatus.csv
If ($CurrentStatus -eq "SUCCESS")
{
$count = 0
}
Else
{
$count += 1
}
Set-Content -Path count.txt -Value $Count
$Modulo = $count % 4
#####################################################
### Send out SMS!! ###
#####################################################
$body=""
$Needtosend="False"
foreach ( $Server in $Serverlist)
{
if (($Server.LastStatus -eq "FAILURE") -or ($Server.CurrentStatus -eq "FAILURE"))
{
$Needtosend="True"
$body=$body+$server.Name+","+$Server.CurrentStatus+","+$server.Updatetime+". "
}
}
If (($Needtosend -eq "True") -and (($Count -le 1) -or ($Modulo -eq 2 )))
{
$emailTo = get-content -path InfoRecipients.txt
$subject = "Mail Server testing Alert."
$smtpServer = "smtpserver.xxxamericaxx.com"
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom, $emailTo, $subject, $body)
Set-Content -Path LastResult.txt -Value $body
}