Monday, March 9, 2015

Windows Server Service Monitroing via E-Mail notification

Save the following powershell script to your server. Edit the underlined values matching to your environment.


 function sendMail{

     Write-Host "Sending Email"

     #SMTP server name
     $smtpServer = "smtp01.domain.com"

     #Creating a Mail object
     $msg = new-object Net.Mail.MailMessage

     #Creating SMTP server object
     $smtp = new-object Net.Mail.SmtpClient($smtpServer)

     #Email structure
     $msg.From = "server01.edomain.com@domain.com"
     $msg.To.Add("serveradmin@domain.com")
     $msg.subject = "server01 - ServiceX stopped"
     $msg.body = "ServiceX stopped on server01. Please check the server state."

     #Sending email
     $smtp.Send($msg)

}

#Calling function
sendMail


After that open the services.msc and browse to the service you want to monitor. Right-click the service, select "Properties" and click on the "Recovery" tab. On the third drop-down menu select "Run a Program" and select the saved powershell script.

No comments:

Post a Comment