?
Solved

VB Script to check services

Posted on 2012-03-27
3
Medium Priority
?
457 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 7

Accepted Solution

by:
HaiFai earned 1500 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month9 days, 9 hours left to enroll

762 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