Solved

VB Script to check services

Posted on 2012-03-27
3
450 Views
Last Modified: 2012-03-27
I have modified this script: -
http://themonitoringguy.com/scripts-code/monitoring-windows-services-vbscript/

If I create a bat file: "CSCRIPT ServiceCheckScript.vbs SERVERNAME "DNS Server"
It outputs a text file showing the status of the DNS service: -

C:\Monitor>CSCRIPT ServiceCheckScript.vbs SIDNT2 "DNS Server"
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.

Service: SERVERNAME DNS Server Running


Does anyone know how I can change the script to monitor services that are NOT running?

Original script below: -
'Declare Variables
Dim objWMIService, objProcess, colProcess, Status, strComputer, strService
 
'Assign Arguments
strComputer = WScript.Arguments(0)
strService = WScript.Arguments(1)
Status= false
 
'Check For Arguments - Quit If None Found
If Len(strService) < 1 Then
    Wscript.echo "No Arguments Entered - Exiting Script"
    WScript.Quit
End If
 
'Setup WMI Objects
Set objWMIService = GetObject("winmgmts:"& "{impersonationLevel=impersonate}!\\" & strComputer & "\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(strComputer) & " " & strService & " Running"
    'Perform Some Pass Logic Here
Else
    Wscript.echo "Service: " & UCase(strComputer) & " " & strService & " Not Running"
    'Perform Some Failed Logic Here
End If
0
Comment
Question by:HKFuey
  • 2
3 Comments
 
LVL 7

Accepted Solution

by:
HaiFai earned 500 total points
Comment Utility
Hi
there you go ... usage script.vbs <computername>
example script.vbs localhost


Dim objWMIService, objProcess, colProcess, Status, strComputer, strService
strComputer = WScript.Arguments(0)
Set objWMIService = GetObject("winmgmts:"& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
Set colProcess = objWMIService.ExecQuery ("SELECT DisplayName, Status, State FROM Win32_Service")
For Each objProcess in colProcess
If 	objProcess.State = "Running" Then
' Wscript.echo "Service: " & UCase(strComputer) & " " & objProcess.DisplayName & " Running"
Else 
Wscript.echo "Service: " & UCase(strComputer) & " " & objProcess.DisplayName & " Not Running"
 End If
Next
 

Open in new window

0
 

Author Comment

by:HKFuey
Comment Utility
Ok thanks, easier than I thought! I now get an email if DNS fails! Cool.

If Status = true Then
    Wscript.echo "Service: " & UCase(strComputer) & " " & strService & " Running"

Else
    Wscript.echo "Service: " & UCase(strComputer) & " " & strService & " Not Running"
    'Perform Some Failed Logic Here
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run("%comspec% /K EmailFailed.bat"), 1, True
End If
0
 

Author Closing Comment

by:HKFuey
Comment Utility
Thanks, I just want to monitor DNS so I created a bat file with this: -
CSCRIPT ServiceCheckScript.vbs MYSERVERNAME "DNS Server"
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
This video discusses moving either the default database or any database to a new volume.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now