Best Way to Monitor Services in Windows Server 2008/2012 (No Install Required)

Recently I faced a challenge where I had to setup a system to monitor a service running on one of systems running operating system Windows Server 2012. In this article I will describe the approach used to monitor a service running on Window Server 2008/2012 without installing any kind of third party applications. Before we proceed with the setup, please gather following details and keep them ready with you:

  • Service name
  • SMTP server
  • SMTP port

Service Name

Service name can be obtained from Services application in Windows. To launch this application, go to run and execute services.msc command. From the Services application, note down the exact Name of the service to be monitored. For this example I’m using the service name as DHCP Client.

services-dhcp-client-monitor

 

SMTP Server and Port

Consult with your network team to obtain the SMTP server and port. Default port for SMTP server is 25. SMTP server and port are required to trigger email alerts.

Configuration

To configure the alert we need to first create a script, store it on the local system and schedule it using Windows Task Scheduler.

Script

Following is the VB script we will be using to monitor a service in Windows Server 2008/2012.

Change the Service Name, SMTP server and port, from, to and cc email addresses according to your settings:

'Declare Variables
Dim objWMIService, objProcess, colProcess, Status, strHost, strService
 
'Define Variables
strHost = "LOCALHOST"
strService = "DHCP Client"
Status= false
 
'Check For Service name - Quit if not defined
If Len(strService) < 1 Then
    Wscript.echo "Please enter a service name"
    WScript.Quit
End If
 
'Setup WMI Objects
Set objWMIService = GetObject("winmgmts:"& "{impersonationLevel=impersonate}!\\" & strHost & "\root\cimv2") 
Set colProcess = objWMIService.ExecQuery ("SELECT DisplayName, Status, State FROM Win32_Service WHERE DisplayName = '" & strService & "'")
 
'Check For Running Service
For Each objProcess in colProcess
    If InStr(objProcess.DisplayName,strService) > 0 And objProcess.State = "Running" Then
	Status = true
    End If
Next
 
If Status = true Then
    Wscript.echo "Service: " & UCase(strHost) & " " & strService & " Running"
    'If you wish to configure an alert for uptime messages, do it here
Else
    Wscript.echo "Service: " & UCase(strHost) & " " & strService & " Not Running"
    'Now we know that service is not running, let us trigger an alert email
	Dim objMessage
	Set objMessage = CreateObject("CDO.Message") 
	objMessage.Subject = "DHCP Client is not running on SERVERNODE" 'Replace SERVERNODE with hostname of your server 
	objMessage.From = "alert@nimishprabhu.com" 'Set From email address
	objMessage.To = "support@nimishprabhu.com" 'Set To email address
	objMessage.Cc = "admin@nimishprabhu.com" 'Set CC email address
	objMessage.HTMLBody = "Hello Support Team

DHCP Client service is not running on Web Server SERVERNODE.

Please check and take a corrective action.

Thanks & Regards

Nimish Prabhu" objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.nimishprabhu.com" 'SMTP server objMessage.Configuration.Fields.Item _ ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 'SMTP port objMessage.Configuration.Fields.Update objMessage.Send End If

Save this script with a .vbs extension (not .txt)

Store the script at some location for e.g. E:\DHCP\ServiceMonitor.vbs

service-monitor-vbscript

Windows Task Scheduler

Open Task Scheduler (Schedule Tasks from Control Panel)

task-scheduler-windows

Create new folder for your task

task-scheduler-windows-new-folder

Click on the newly created folder and click on Create Task from Actions window pane on right side.

task-scheduler-windows-create-task

 

In General tab, fill in the name of the task, Under security options select “Run whether user is logged in or not” and check the checkbox besides Run with highest privileges.

Create-task-general-settings

In Trigger tab, create a new trigger and set it run daily with an option to repeat task every 5mins indefinitely. Click Ok.

Create-task-trigger-settings

 

Create-task-trigger-settings-2

In actions tab, create new action to Start a program, with Program location as C:\Windows\System32\cscript.exe

In add arguments section specify the path to the VB script for e.g. E:\DHCP\ServiceMonitor.vbs

Click Ok.

Create-task-action-settings

Create-task-action-settings-2

In condition tab, keep the default settings (modify them if you know what you are doing)

Create-task-conditions-settings

Create-task-settings-tab

 

Click Ok. A prompt will popup to enter your credentials. Enter the same and click OK.

A task will be created and will run every 5min and send you alerts incase the service being monitored stops running:

Create-task-final

That’s all folks!

Let me know if you liked this article and it helped you. Also, if you have any questions related to monitoring services in Windows Server 2008/2012, please use the comment form below to post the same.

7 thoughts on “Best Way to Monitor Services in Windows Server 2008/2012 (No Install Required)”

  1. Vairamuthu S says:

    Hi Nimish,

    Your article is helpful for me, but please add a recommend that we can use this script for one service or multiple services ?

    And Is this script works for some additional (3rd party) services ?

  2. Alexander Gold says:

    what needs to be added if you need to login to your smtp server on port 587?

    1. NIMISH says:

      Try adding these lines:
      objMessage.Configuration.Fields.Item _
      (“http://schemas.microsoft.com/cdo/configuration/smtpauthenticate”) = 1 ‘Basic Authentication
      objMessage.Configuration.Fields.Item _
      (“http://schemas.microsoft.com/cdo/configuration/sendusername”) = “alert@nimishprabhu.com”
      objMessage.Configuration.Fields.Item _
      (“http://schemas.microsoft.com/cdo/configuration/sendpassword”) = “yourpassword”
      Before:
      objMessage.Configuration.Fields.Update
      objMessage.Send

  3. Ted says:

    Hi, when I run the script I get the error “c:\HylandUnitySvc.vbs(38, 43) Microsoft compilation error: Unterminated string constant

    1. NIMISH says:

      Please share your script. Line 38 in above script is:

      objMessage.HTMLBody = “Hello Support Team

      Which does not seem to the reason for error.

  4. Pervaiz IT says:

    hello Sir,

    this good but my smtp required authentication kindly suggest .

    1. NIMISH says:

      Try adding these lines:
      objMessage.Configuration.Fields.Item _
      (“http://schemas.microsoft.com/cdo/configuration/smtpauthenticate”) = 1 ‘Basic Authentication
      objMessage.Configuration.Fields.Item _
      (“http://schemas.microsoft.com/cdo/configuration/sendusername”) = “alert@nimishprabhu.com”
      objMessage.Configuration.Fields.Item _
      (“http://schemas.microsoft.com/cdo/configuration/sendpassword”) = “yourpassword”
      Before:
      objMessage.Configuration.Fields.Update
      objMessage.Send

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.