Script to check if a windows service is running, and email a user if it is not

This script will check if a specified windows service is running, and if not, it will email a user.

This script uses the Microsoft CDO service. You will need to ensure you have permissions to relay through the SMTP server you have specified in the variables.


'check service.vbs
'/*****************************************************************/
' ABOUT:
' Checks if a service is running and emails user if it is not
'
' USAGE:
' Put this script in as a windows scheduled task
' Create and modify a copy of this file for each service you wish to monitor.
' You must change the variables below to your own values
'
' Written by Alan Byrne - 20/02/2007
'/**********************************************************/

dim objShell, objNet
dim strToEmailAddress, strFromEmailAddress, strServiceNameToCheck
dim strSMTPServer, strSMTPServerPort
dim strComputerName

Set objNet = CreateObject("WScript.NetWork")

' **** Set Variables here *****
strToEmailAddress = "alanb@domain.com"
strFromEmailAddress = "administrator@domain"
strServiceNameToCheck = "TermService"
strSMTPServer = "192.168.1.2"
strSMTPServerPort = "25"

set objShell = CreateObject("Shell.Application")

If objShell.IsServiceRunning(strServiceNameToCheck) = false then
Set objMessage = CreateObject("CDO.Message")

'Send Message
objMessage.Subject = "Service Failed: " & strServiceNameToCheck
objMessage.From = "Service Failure Notice "
objMessage.To = strToEmailAddress
objMessage.TextBody = "The service '" & strServiceNameToCheck & "' failed on " & objNet.ComputerName & vbcrlf & "Time: " & Now
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strSMTPServer
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = strSMTPServerPort
objMessage.Configuration.Fields.Update
objMessage.Send
End if

set objShell = nothing
set objNet = Nothing



7 Responses to “Script to check if a windows service is running, and email a user if it is not”

  1. Jon Gerdes says:

    Great script!

    Note that this website has “sanitised” this script. It has turned instances of into amp, so make sure you change them back otherwise you will get an error.

    To get a list of services, run “sc query” in a command shell and use the SERVICE_NAME:

    Cheers
    Jon

  2. [...] product page Jigsaw Boys spaceFetcher * * * * * * * * Sites you may be interested in Jigsaw Boys Misc Script to check if a windows service is running, and email a user if it is not Tags &gt No Tags &lt This product is also listed in Business to Business New Products Web [...]

  3. Christy says:

    Still useful after all these years.

  4. Really nice blog you have here. I found you via google. I am going to add you to my feed reader.

  5. How do I add this to my RSS reader? Sorry I’m a newbie :(

  6. to domain name says:

    hi guys…

    hi guysI would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well and i have start my own blog now, , thanks for your effort…

  7. vik says:

    @Dinesh:

    Try this: sc query > services.xls

    Yes, you can change the filename (and extention too) as per the requirment.(dinesh.txt, vik.doc etc)

Leave a Reply

You must be logged in to post a comment.