Send alert for Windows Service Error

First, we need to create the Powershell script (Source from HowToGeek.com):

$EmailFrom = "emailID@emaildomain.com"
$EmailTo = "destination@somedomain.com"
$Subject = "The subject of your email"
$Body = "What do you want your email to say"
$SMTPServer = "smtp.email.server.address"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("usr", "pass");
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)

Then we need a script to invoke the powershell script

powershell & "C:\PathToPowerShellScript\myPowerShellEmailScript.ps1"

Finally, modify the service to execute a command on failure:

SC failure serviceName command="C:\PathToBat\MyScript.bat"

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s