Jump to content

WMI SQL query results object can only be used once


Recommended Posts

I have code that does a WMI SQL query to find all defined printers, and I want to parse the returned object in several places.  However, after parsing it the first time, all other times fail to find any printer objects.

Here is my test code:

test()

Func test()
    Local $oPrinters, $oPrinter, $err, $cnt, $oP, $query

    $query = "SELECT * FROM Win32_Printer"
    $oPrinters = doQuery($query)

    $err = @error
    LogMsg("+++: $err = " & $err & ", isObj($oPrinters) = " & IsObj($oPrinters))

    If ($err == 0) Then

        LogMsg("FIRST LOOP") ; <=== FIRST LOOP
        $cnt = 0
        $oP = $oPrinters
        LogMsg("+++: isObj($oP) = " & IsObj($oP))

        For $oPrinter In $oP
            $cnt += 1
            LogMsg("+++: isObj($oPrinter): " & IsObj($oPrinter) & ", $oPrinter.Name ==>" & $oPrinter.Name & "<==")
        Next
        LogMsg("+++: Found " & $cnt & " printers")

        LogMsg("SECOND LOOP") ; <== SECOND LOOP
        $cnt = 0
        $oP = $oPrinters
        LogMsg("+++: isObj($oP) = " & IsObj($oP))

        For $oPrinter In $oP
            $cnt += 1
            LogMsg("+++: isObj($oPrinter): " & IsObj($oPrinter) & ", $oPrinter.Name ==>" & $oPrinter.Name & "<==")
        Next
        LogMsg("+++: Found " & $cnt & " printers")
    EndIf

EndFunc   ;==>test

Func doQuery($sQuery, $lnum = @ScriptLineNumber)
    #forceref $lnum
    LogMsg("+++:" & $lnum & ": doQuery(" & '"' & $sQuery & '"' & ") entered")
    Local $oWMIService, $oResults, $errstr
    Local $wbemFlags = BitOR(0x20, 0x10) ; $wbemFlagReturnImmediately and wbemFlagForwardOnly

    $oWMIService = ObjGet("winmgmts:\\" & "localhost" & "\root\CIMV2")

    If (IsObj($oWMIService)) Then

        $oResults = $oWMIService.ExecQuery($sQuery, "WQL", $wbemFlags)

        If (IsObj($oResults)) Then
            LogMsg("+++: doQuery() returns @error = 0, Good: returning the object")
            Return (SetError(0, 0, $oResults)) ;;; Good: return the object
        Else
            $errstr = "" _
                     & "WMI Query failed." & @CRLF _
                     & "This is the query:" & @CRLF _
                     & "   " & $sQuery
            LogMsg("+++: ====>" & $errstr & "<===")
            LogMsg("+++: doQuery() returns @error = 1")
            Return (SetError(1, 0, $errstr)) ; Error: Query faled
        EndIf
    Else
        $errstr = "" _
                 & "WMI Output" & @CRLF _
                 & "No WMI Objects Found for class: " & @CRLF _
                 & "Win32_PrinterDriver" & @CRLF _
                 & "using this query:" & @CRLF _
                 & "   " & $sQuery
        LogMsg("+++: ====>" & $errstr & "<===")
        MsgBox(0, "ERROR", $errstr) ; Error: Cannot get $oWMIService object
        Exit (1)
    EndIf
EndFunc   ;==>doQuery

Func LogMsg($msg, $lnum = @ScriptLineNumber)
    ConsoleWrite("+++:" & $lnum & ": " & $msg & @CRLF)
EndFunc   ;==>LogMsg

Parsing the returned $oPrinters object shows 5 printers:

+++:15: FIRST LOOP
+++:18: +++: isObj($oP) = 1
+++:22: +++: isObj($oPrinter): 1, $oPrinter.Name ==>Microsoft XPS Document Writer<==
+++:22: +++: isObj($oPrinter): 1, $oPrinter.Name ==>Microsoft Office Document Image Writer<==
+++:22: +++: isObj($oPrinter): 1, $oPrinter.Name ==>Fax<==
+++:22: +++: isObj($oPrinter): 1, $oPrinter.Name ==>Canon MG7100 series Printer WS<==
+++:22: +++: isObj($oPrinter): 1, $oPrinter.Name ==>Canon MG6100 series Printer WS<==
+++:24: +++: Found 5 printers

Parsing it again, shows no printers:

+++:26: SECOND LOOP
+++:29: +++: isObj($oP) = 1
+++:35: +++: Found 0 printers

 

Link to comment
Share on other sites

Maybe because the "cursor" is at the end of the resulting record set returned by the query.
You could try to loop throug $oP this way:

For $i = 1 to $oP.count
   ; Process $oP($i)
Next

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Right.  When I set the wbem flags to 0 (wait for complete) and went back to my original code, I was able to make multiple passes of the $oPrinters object.  However, no matter what I tried, I could not access one of the printer objects in $oPrinters using an index, like $oPrinters[2].

So it's fixed, thank you,

Link to comment
Share on other sites

:)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...