none
WMI VBScript results error RRS feed

  • Question

  • 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


    Tuesday, February 4, 2014 4:47 PM

Answers

  • 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 issue.


    ¯\_(ツ)_/¯


    • Edited by jrv Tuesday, February 4, 2014 7:24 PM
    • Marked as answer by Bill_StewartModerator Monday, February 24, 2014 7:22 PM
    Tuesday, February 4, 2014 7:23 PM

All replies

  • 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

    Tuesday, February 4, 2014 5:10 PM
    Moderator
  • 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 issue.


    ¯\_(ツ)_/¯


    • Edited by jrv Tuesday, February 4, 2014 7:24 PM
    • Marked as answer by Bill_StewartModerator Monday, February 24, 2014 7:22 PM
    Tuesday, February 4, 2014 7:23 PM