Solved

VB Script to check services

Posted on 2012-03-27
3
453 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

831 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