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

By Burnsie • Feb 21st, 2007 • Category: Misc

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

Related posts:

  1. VB Script to check disk space and email results


FREE NEWSLETTER -> Want Tech Tips Sent Straight to your Inbox?

Grab our Newsletter to Ensure your PC is Running Smooth!

Burnsie is currently working as a Wintel Messaging Engineer for a large commercial bank specialising in Microsoft Exchange and Blackberry administration for 1000+ users.
Email this author | All posts by Burnsie

2 Responses »

  1. 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 [...]

Leave a Reply