WMI VBScript results error

I have written the following script using scriptomatic to check for the existence of a specific service where I have substituted my-computer in the array with the actual list of computer names.  The script runs as expected and produces the output to a text file.  The settings are as follows- 

  • The WMI Namespace is set to root\CIMV2 
  • The WMI Class is set to Win32_Service. 
  • Language is set to VBScript 
  • Output format is plain text

My problem/question is with the results.  I have found a computer that according to the results should have the service running.  When I actually verified the service, it does not exist on the computer.(I am checking Win2k3 and Win2k8 servers)  I am not sure why.  Thoughts?  Thanks.

On Error Resume Next

Const wbemFlagReturnImmediately = &h10

Const wbemFlagForwardOnly = &h20

arrComputers = Array("my-computer")

For Each strComputer In arrComputers

   WScript.Echo

   WScript.Echo "=========================================="

   WScript.Echo "Computer: " & strComputer

   WScript.Echo "=========================================="

   Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")

   Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Service where DisplayName = ""SplunkForwarder""")

   For Each objItem In colItems

      WScript.Echo "DisplayName: " & objItem.DisplayName

      WScript.Echo "Started: " & objItem.Started

      WScript.Echo

   Next

Next

Function WMIDateStringToDate(dtmDate)

WScript.Echo dtm:

                WMIDateStringToDate = CDate(Mid(dtmDate, 5, 2) & "/" & _

                Mid(dtmDate, 7, 2) & "/" & Left(dtmDate, 4) _

                & " " & Mid (dtmDate, 9, 2) & ":" & Mid(dtmDate, 11, 2) & ":" & Mid(dtmDate,13, 2))

End Function


February 4th, 2014 7:47pm

Step one is to remove On Error Resume Next from the top of your script.

Don't use that line of code unless you understand exactly what it does and how it works.

Bill

Free Windows Admin Tool Kit Click here and download it now
February 4th, 2014 8:10pm

Definitely follow Bill's advice.

I would also suggest starting with deleting every line of code that is not needed to get the basic results.  You can add the fancy stuff after you understand how this works.

Example:

strComputer = "mycomputer"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")

Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Service where DisplayName='SplunkForwarder'")
For Each objItem In colItems
    WScript.Echo "DisplayName: " & objItem.DisplayName
    WScript.Echo "Started: " & objItem.Started
Next


Whenever you have an issue always reduce down to the least amount of code necessary to test you

February 4th, 2014 10:23pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics