Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

VB Script to check services

Posted on 2012-03-27
3
Medium Priority
?
458 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Script to copy or move mouse-selected collection of files plus targets referenced by shortcuts (.lnk) The purpose of this article is to help illuminate the real challenges and options available (where they may exist) for utilizing simple scriptin…
This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

647 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