The script basically uses telnet to check port 25. I didn't bother with the telnet commands because what I discovered was that if hmailserver is either STOPPED or PAUSED, the return value will be a timeout (actively refused connection on ip.ad.dr.ess:25). I figure this way, it doesn't matter what caused the malfunction because we're looking for the outcome to tell us that something is wrong.
It checks status 3 times with 5 minutes inbetween before taking action. If after the third try, its still not possible to communicate with port 25, it will attempt to restart the service and also send a notification. Then it will wait 60 seconds and check service status. If the service is not running, it will send another notification.
Obviously, if there is a problem with hmailserver, you won't be able to send an email via hmailserver, so notifications will be sent via outside smtp service. I tested it with gmail. I also send notifications via my sms gateway. Just delete that part if you don't want to use it.
Run from task scheduler.
hMSStatusActionNotify.ps1
Code: Select all
$ErrorActionPreference = 'silentlycontinue'
$remotehost = "localhost"
$port = 25
$ServiceName = 'hMailServer'
$EmailFrom = "user@gmail.com"
$EmailTo = "user@gmail.com"
$SMTPServer = "smtp.gmail.com"
$SMTPAuthUser = "user@gmail.com"
$SMTPAuthPass = "TopSecretPassword"
# Open telnet
$socket = new-object System.Net.Sockets.TcpClient($remoteHost, $port)
# If connection refused, wait X seconds and try again
if($socket -eq $null) {
Start-Sleep -seconds 300
$socket = new-object System.Net.Sockets.TcpClient($remoteHost, $port)
# If connection refused, wait X seconds and try again
if($socket -eq $null) {
Start-Sleep -seconds 300
$socket = new-object System.Net.Sockets.TcpClient($remoteHost, $port)
# If connection refused, restart HMS service and send notification about restart
if($socket -eq $null) {
Restart-Service $ServiceName
# Send Notification via Gmail
$Subject = "Windows Service Failure"
$Body = "ATTENTION! MyServer reporting that the hMailServer service is being RESTARTED due to a fault. Check status NOW."
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($SMTPAuthUser, $SMTPAuthPass);
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
# Send Notification via SMS
$uri = "https://smsgateway/index.php/plugin/rest_api/send_sms?phoneNumber=1234567890&message=ATTENTION! hMailServer service is being RESTARTED due to a fault. Check status NOW."
$response = Invoke-RestMethod -Uri $uri
# Wait X seconds, check status of service - if not running, give up trying and send notification re service fault
Start-Sleep -seconds 60
(get-service $ServiceName).Refresh()
if ((get-service $ServiceName).Status -ne 'Running')
{
# Send Notification via Gmail
$Subject = "Windows Service Failure"
$Body = "ATTENTION! MyServer reporting that the hMailServer service is not running. Check status NOW."
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($SMTPAuthUser, $SMTPAuthPass);
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
# Send Notification via SMS
$uri = "https://smsgateway/index.php/plugin/rest_api/send_sms?phoneNumber=1234567890&message=ATTENTION! MyServer reporting that the hMailServer service is not running. Check status NOW."
$response = Invoke-RestMethod -Uri $uri
}
}
}
}