Jump to content

Documentation on $oWMIService.ExecQuery


Trax
 Share

Recommended Posts

Hi @Trax

ExecQuery is not a function, it's a method linked to the object it is used with, in your case a WMIService object.

This means you can't use ExecQuery with every objects. You can only use it with objects that include this method.

When an object include this method, you can find the parameters of ExecQuery in microsoft libraries, like this one:

https://msdn.microsoft.com/en-us/library/aa393866(v=vs.85).aspx

But it doesn't mean that all ExecQuery method will use the same parameters, as it can change relating the object it is associated to. 

If you want to know the exact parameters, you need to refer to the library of the object you want to interact with :)

Link to comment
Share on other sites

I suggest to check the Example Scripts forum for Scriptomatic. That's a GUI which lets you create WMI scripts in AutoIt.

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

   Thanks guys. My problem wasn't the WMI Query it was displaying the results. A bonehead move on my part. The code works. I have a For=>Next loop I don't need. Even if the WMI Query returns several items I am only interested in the first one. I just can't figure out the syntax to get rid of the For/Next loop:

Func FindScanner()
    Local $NumericPort, $TestDevice, $KeyName, $PortName
    Local $oWMIService = ObjGet("winmgmts:\\localhost\root\CIMV2")
    $CommPort = ""
    If @error Then
        Return SetError(@error, 0, "")
    EndIf
    Local $ColItems = $oWMIService.ExecQuery("SELECT * FROM Win32_PnPEntity WHERE Name LIKE 'My Unique Serial Port%'", "WQL", 48)
    If Not IsObj($ColItems) Then
        Return
    EndIf
    For $ColItem In $ColItems
        $KeyName = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\" & $colItem.DeviceID & "\Device Parameters"
        $PortName = RegRead($KeyName, "PortName")
        If @error Then
            ContinueLoop
        EndIf
        $NumericPort = Int(StringReplace($PortName, "COM", ""))
        $CommPort = $PortName
        If ($NumericPort > 9) Then
            $CommPort = "\\.\" & $CommPort
        EndIf
        ExitLoop
    Next
    EndFunc

 

Edited by Trax
Link to comment
Share on other sites

Your script is looking for hardware in your computer with names containing "COM".

As COM (serial) ports are usually old and not included with recent computers, if your computer does not contains one, the query will obviously not return anything.

Are you sure that your computer has a COM (serial) port? It looks like this:

_SRPRUSB.JPG

And how do you want to match a com (serial) port to an audio hardware?

Link to comment
Share on other sites

See anything wrong with this statement? Typo?

$Result = FindScanner()
If $Results = -1 Then
    Exit
EndIf


Here's an example which has been tested:

 

 

Local $_objError = ObjEvent('AutoIt.Error', '_objErrorHandler')
;
Local $sReturn = Example()
MsgBox(0, 'Results', $sReturn)
Exit
;
Func Example()
    Local $objWMI = ObjGet('winmgmts:\\localhost\root\CIMV2')
    Local $objItems = $objWMI.ExecQuery('SELECT * FROM Win32_PnPEntity WHERE Name LIKE "%COM%"')
    If @error Then
        Return 'Object Error'
    ElseIf $objItems.count = 0 Then
        Return 'No Objects Found'
    Else
        Return 'Objects Found: ' & $objItems.count
    EndIf
EndFunc
;
Func _objErrorHandler($_objError)
    $_objError.Clear
    Return SetError(1)
EndFunc
;

 

 

 

Edited by ripdad
typo

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...