Solved

VB Script to check services

Posted on 2012-03-27
3
451 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
ID: 37770613
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
ID: 37770897
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
ID: 37770910
Thanks, I just want to monitor DNS so I created a bat file with this: -
CSCRIPT ServiceCheckScript.vbs MYSERVERNAME "DNS Server"
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

912 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

15 Experts available now in Live!

Get 1:1 Help Now