Irios

How to enumerate all properties in an object variable (wmi) when I don't know their names? [SOLVED!]

15 posts in this topic

#1 ·  Posted (edited)

First post here, please be gentle ;)

 

I've spent hours and hours trying to figure out this puzzle. It's impossible to google for because I don't know the precise terminology to use. All msdn links I find are either dead or has been removed like 10 years ago.

 

Here is a tiny code example:

$objWMIService = ObjGet("winmgmts:")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_PerfRawData_Tcpip_NetworkInterface Where Name like '%intel%'")
For $objItem In $colItems
    ConsoleWrite($objItem.PacketsPersec & @CRLF)
Next

In that example I'm using the asterix * and it retrieves all properties in the "Win32_PerfRawData_Tcpip_NetworkInterface" class.  But I'm only utilizing "PacketsPersec" in the example.

How could I enumerate all the properties without defining their names beforehand?

(FYI, I do know their names, but the script doesn't know the names)

In other words; how can I use "SELECT *" on any class and output all properties, one by one? Without having to manually specify each property with $objItem.<propertyname> ?


As comparison, using wmic in a cmd shell ...

    wmic path Win32_PerfRawData_Tcpip_NetworkInterface  Where "Name like '%intel%'" get /value

...and it lists all properties in "Names=Value" manner.

This is sort of the same function I'm looking for. How could I do the same in Autoit?

 

 

EDIT: I cannot run a powershell or cmd  to "solve" the problem.  I need to find a way to do this "internally" in Autoit. I.e. by calling the WMI service directly with ObjGet(winmgmts:) or connect to the WMI system with CreateObject("WbemScripting.SWbemLocator"). Performance is a major factor.

Edited by Irios
Solved! :D
1 person likes this

Share this post


Link to post
Share on other sites



Local $strComputer = "."
Local $strNameSpace = "root\cimv2"
Local $strClass = "Win32_PerfRawData_Tcpip_NetworkInterface"

Local $objClass = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $strComputer & "\" & $strNameSpace & ":" & $strClass)
For $objClassProperty In $objClass.Properties_
    ConsoleWrite($objClassProperty.Name & @CRLF)
Next

 


Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Share this post


Link to post
Share on other sites

Hi there, Terenz.

I must admit I already had a look into ".Properties_" earlier on, but I don't understand its inner workings, tbh. Or rather, I don't know how to get BOTH the property names AND values... in one go.

In your script example... if you output "$objClass.Value" as well, it is just empty. (I'm guessing it's not really the Value for the property at all)

; ../..
ConsoleWrite($objClassProperty.Value & @CRLF)
; ../..

 

Is it a different object "type" altogether? This is my problem, I'm having a hard time getting a grasp on these objects (where do I start to read about this?), and how are they structured internally? (Do you have any simple examples to illustrate this? Or an info page link?)

I could use your script example combined with my own script to get both property names and their respective values, but it's not very efficient and you're then doing two wmi calls. There must be a way to do this in a single run.

Share this post


Link to post
Share on other sites

Hi @Irios perhaps you can try WMI Explorer. Its a tool for displaying WMI Namespaces, Classes and Properties. WMI Explorer
A Way to get both the property and value written to the console is in the example below.

Local $objWMIService = ObjGet("winmgmts:\\" & "." & "\root\CIMV2")
Local $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_PerfRawData_Tcpip_TCP")

If IsObj($colItems) then
   For $objItem In $colItems
      ConsoleWrite ("Caption: " & $objItem.Caption & @CRLF )
      ConsoleWrite ("ConnectionFailures: " & $objItem.ConnectionFailures & @CRLF )
      ConsoleWrite ("ConnectionsActive: " & $objItem.ConnectionsActive & @CRLF )
      ConsoleWrite ("ConnectionsEstablished: " & $objItem.ConnectionsEstablished & @CRLF )
      ConsoleWrite ("ConnectionsPassive: " & $objItem.ConnectionsPassive & @CRLF )
      ConsoleWrite ("ConnectionsReset: " & $objItem.ConnectionsReset & @CRLF )
      ConsoleWrite ("Description: " & $objItem.Description & @CRLF )
      ConsoleWrite ("Frequency_Object: " & $objItem.Frequency_Object & @CRLF )
      ConsoleWrite ("Frequency_PerfTime: " & $objItem.Frequency_PerfTime & @CRLF )
      ConsoleWrite ("Frequency_Sys100NS: " & $objItem.Frequency_Sys100NS & @CRLF )
      ConsoleWrite ("Name: " & $objItem.Name & @CRLF )
      ConsoleWrite ("SegmentsPersec: " & $objItem.SegmentsPersec & @CRLF )
      ConsoleWrite ("SegmentsReceivedPersec: " & $objItem.SegmentsReceivedPersec & @CRLF )
      ConsoleWrite ("SegmentsRetransmittedPersec: " & $objItem.SegmentsRetransmittedPersec & @CRLF )
      ConsoleWrite ("SegmentsSentPersec: " & $objItem.SegmentsSentPersec & @CRLF )
      ConsoleWrite ("Timestamp_Object: " & $objItem.Timestamp_Object & @CRLF )
      ConsoleWrite ("Timestamp_PerfTime: " & $objItem.Timestamp_PerfTime & @CRLF )
      ConsoleWrite ("Timestamp_Sys100NS: " & $objItem.Timestamp_Sys100NS & @CRLF )
   Next
Else
    Msgbox (48, "WMI Error", "WMI Error")
Endif

 

1 person likes this

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Hi pluto41,

Like I wrote in my initial post, I need a way to do it without having to define all the property names beforehand in the code. (Your example will only output properties you specifically define in the code). I want to gather all properties (SELECT *), no matter the name, and then output the Name and its respective Value.

 

EDIT: You refer to WMI Explorer 2... if you use the PowerShell output there... See how it's done with PS? The script actually parses the WMI data and displays it as "Property : Value"... without having to define each Property first. That's  what I'm looking for in AutoIt.

 

WMI Explorer 2 Powershell example:

$computer = $env:COMPUTERNAME
$namespace = "ROOT\CIMV2"
$classname = "Win32_PerfRawData_Tcpip_NetworkInterface"

 

Get-WmiObject -Class $classname -ComputerName $computer -Namespace $namespace |

    Select-Object * -ExcludeProperty PSComputerName, Scope, Path, Options, ClassPath, Properties, SystemProperties, Qualifiers, Site, Container |
    Format-List -Property [a-z]*

 

 

 

 

WMI Explorer 2 Powershell example output:

BytesReceivedPersec             : 105906295
BytesSentPersec                 : 1472390918
BytesTotalPersec                : 1578297213
Caption                         :
CurrentBandwidth                : 1000000000
Description                     :
Frequency_Object                : 0
Frequency_PerfTime              : 3521210
Frequency_Sys100NS              : 10000000
Name                            : Intel[R] 82579V Gigabit Network Connection
OffloadedConnections            : 0
OutputQueueLength               : 0
PacketsOutboundDiscarded        : 0
PacketsOutboundErrors           : 0
PacketsPersec                   : 1440415
PacketsReceivedDiscarded        : 0
PacketsReceivedErrors           : 0
PacketsReceivedNonUnicastPersec : 23105
PacketsReceivedPersec           : 313482
PacketsReceivedUnicastPersec    : 290377
PacketsReceivedUnknown          : 0
PacketsSentNonUnicastPersec     : 3858
PacketsSentPersec               : 1126933
PacketsSentUnicastPersec        : 1123075
Timestamp_Object                : 0
Timestamp_PerfTime              : 13100603957
Timestamp_Sys100NS              : 131179061249680000

Edited by Irios

Share this post


Link to post
Share on other sites

@Irios if you're looking for both the property name and the value in a single pass I would do something like this:

Local $oWMI = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Local $oItems = $oWMI.ExecQuery("Select * FROM Win32_PerfRawData_Tcpip_NetworkInterface")
    For $sItem In $oItems
        For $sProperty In $sItem.Properties_
            ConsoleWrite($sProperty.Name & ": " & $sProperty.Value & @CRLF)

        Next
    Next

 Just be aware that some properties may return an array rather than a string. So you might have to code a For loop to gather all the values.

1 person likes this

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

Oke @Irios I understand what you mean. Very interesting problem. I spend a hour or so to play a bit with the concept but i can't get it to work :( Anyway this is my try.

Local $strComputer = "."
Local $strNameSpace = "root\cimv2"
Local $strClass = "Win32_PerfRawData_Tcpip_NetworkInterface"
Local $objClass = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $strComputer & "\" & $strNameSpace & ":" & $strClass)

Local $objWMIService = ObjGet("winmgmts:\\" & "." & "\root\CIMV2")

Local $colItems
For $objProperty In $objClass.Properties_
    ConsoleWrite ( "=========> " & $objProperty.Name & " " )
    $colItems = $objWMIService.ExecQuery("SELECT " & $objProperty.Name & " FROM Win32_PerfRawData_Tcpip_TCP")
    For $objItem in $colItems
        ; Oke does not work but we are in the loop so something is going on here.
        Local $sValue = Execute ($objItem.Name)     ; attempt to retrieve the contents of $objItem.Name rather then the object.property itself.
        ConsoleWrite ( "Value : " & $sValue & @CRLF )
    Next
Next

 

Share this post


Link to post
Share on other sites

@pluto41 you've already made the call to the class, no need to do it again.


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
28 minutes ago, JLogan3o13 said:

@Irios if you're looking for both the property name and the value in a single pass I would do something like this:

../..

Super thanks, @JLogan3o13

That was exactly what I was looking for!

 

 

 

Share this post


Link to post
Share on other sites

Glad to be of assistance :)

1 person likes this

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

@JLogan3o13 Yup good job! This was exactly where i was after but now i see you use ExecQuery and i used the code posted earlier in the topic.

Local $strComputer = "."
Local $strNameSpace = "root\cimv2"
Local $strClass = "Win32_PerfRawData_Tcpip_NetworkInterface"

Local $objClass = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $strComputer & "\" & $strNameSpace & ":" & $strClass)
For $objClassProperty In $objClass.Properties_
    ConsoleWrite($objClassProperty.Name & @CRLF)
    ConsoleWrite($objClassProperty.Value & @CRLF)   ; does not work!!!
Next

In this case there isn't a .Value so i tried to program around it. Which wasn't a succes :P Thanks for sharing your'e code. Excellent! (y)

Share this post


Link to post
Share on other sites

So, now that I have a solution, I still don't understand how to know where to use ".Properties_" and what I might expect find inside it. I do know in regard of WMI Classes and their Properties, of course. That's what this example was about.

Is there some kind of reference where i can learn about this? You have no idea the amount of time I've spent on this little problem now, and I'm not really getting anywhere. Just copying in a solution given by someone else doesn't really provide the basis for expanding and exploring these objects further.

I've had a look at OleView.Exe, but that doesn't help me at all. Just as stuck. I can't even find any wmi classes or namespaces in there :/

 

Share this post


Link to post
Share on other sites

@pluto41

I've more or less already tried those approaches you suggested, in one way or another. And, yeah you can sort of make it do the job, but not in a very efficient way. I really wanted a way do it "in one go". After all, PS and WMIC could do it. I knew it was possible, I just couldn't figure it out myself.

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

Yeah, the basics of WMI (retrieving data) isn't something I'm struggling with, to be honest. I've been using WMIC and WQL for quite a while, so I know those things pretty good now.

But using objects to retrieve data from WMI is brand new to me. COM, WBEM, etc. I really don't know where to start. It's quite overwhelming, and I figured if I'd just get a few starters by googling some practical examples, I might figure it out by myself. But no, haha. Like you say, I've obviously got a lot of reading ahead of me :D

Edited by Irios
letters and words

Share this post


Link to post
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

  • Similar Content

    • AndyS19
      By AndyS19
      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  
    • jguinch
      By jguinch
      Hello.
      I did create these few functions several months ago. I post here, if it can interest someone.
      These functions based on WMI queries allow you to manage printers : add / delete printer, driver, port, or obtain configuration, set default printer ... I let you discover it with the code.

       
      Here is the list of the available functions :
      _Printmgr_AddLocalPort
      _Printmgr_AddLPRPort
      _PrintMgr_AddPrinter
      _PrintMgr_AddPrinterDriver
      _PrintMgr_AddTCPIPPrinterPort
      _PrintMgr_AddWindowsPrinterConnection
      _PrintMgr_CancelAllJobs
      _Printmgr_EnumPorts
      _PrintMgr_EnumPrinter
      _PrintMgr_EnumPrinterConfiguration
      _PrintMgr_EnumPrinterDriver
      _PrintMgr_EnumPrinterProperties
      _PrintMgr_EnumTCPIPPrinterPort
      _Printmgr_Pause
      _Printmgr_PortExists
      _Printmgr_PrinterExists
      _Printmgr_PrinterSetComment
      _Printmgr_PrinterSetDriver
      _Printmgr_PrinterSetPort
      _Printmgr_PrinterShare
      _Printmgr_PrintTestPage
      _PrintMgr_RemoveLocalPort
      _PrintMgr_RemoveLPRPort
      _PrintMgr_RemovePrinter
      _PrintMgr_RemovePrinterDriver
      _PrintMgr_RemoveTCPIPPrinterPort
      _PrintMgr_RenamePrinter
      _Printmgr_Resume
      _PrintMgr_SetDefaultPrinter
       
      And some examples :
      #Include "PrintMgr.au3" ; Remove a printer called "My old Lexmark printer" : _PrintMgr_RemovePrinter("My old Lexmark printer") ; Remove the driver called "Lexmark T640" : _PrintMgr_RemovePrinterDriver("Lexmark T640") ; Remove the TCP/IP printer port called "TCP/IP" _PrintMgr_RemoveTCPIPPrinterPort("MyOLDPrinterPort") ; Add a driver, called "Samsung ML-451x 501x Series", and driver inf file is ".\Samsung5010\sse2m.inf" _PrintMgr_AddPrinterDriver("Samsung ML-451x 501x Series", "Windows NT x86", @scriptDir & "\Samsung5010", @scriptDir & "\Samsung5010\sse2m.inf") ; Add a TCP/IP printer port, called "MyTCPIPPrinterPort", with IPAddress = 192.168.1.10 and Port = 9100 _PrintMgr_AddTCPIPPrinterPort("MyTCPIPPrinterPort", "192.168.1.10", 9100) ; Add a printer, give it the name "My Printer", use the driver called "Samsung ML-451x 501x Series" and the port called "MyTCPIPPrinterPort" _PrintMgr_AddPrinter("My Printer", "Samsung ML-451x 501x Series", "MyTCPIPPrinterPort") ; Set the printer called "My Printer" as default printer _PrintMgr_SetDefaultPrinter("My Printer") ; Connect to the shared printer "\\192.168.1.1\HPDeskjetColor") _PrintMgr_AddWindowsPrinterConnection("\\192.168.1.1\HPDeskjetColor") ; List all installed printers #Include <Array.au3> $aPrinterList = _PrintMgr_EnumPrinter() _ArrayDisplay($aPrinterList) ; List all printers configuration #Include <Array.au3> $aPrinterConfig = _PrintMgr_EnumPrinterConfiguration() _ArrayDisplay($aPrinterConfig) ; List all installed printer drivers #Include <Array.au3> $aDriverList = _EnumPrinterDriver() _ArrayDisplay($aDriverList) ; Retrieve the printer configuration for the printer called "Lexmark T640" #Include <Array.au3> $aPrinterConfig = _PrintMgr_EnumPrinterConfiguration("Lexmark T640") _ArrayDisplay($aPrinterConfig) ; Add a local printer port (for a file output) _AddLocalPrinterPort("c:\temp\output.pcl") ; Remove the local port _RemoveLocalPrinterPort("c:\temp\output.pcl") Download link :  PrintMgr.au3
    • jguinch
      By jguinch
      Here is an UDF for managing printers.
      Features are :
       - add, remove or rename a printer
       - add or remove a driver
       - add or remove a TCP/IP printer port
       - add or remove a LPR printer port
       - connect to a remote printer
       - enum printers and there configuration and properties
       - pause resume or cancel all jobs of a printer
       - checks if a printer exists
       - print a test page
       - set the default printer
    • ripdad
      By ripdad
      This function (version 1), was developed solely for applications I needed it for.
      It was only tested with Class names that was needed in those scripts.
      And so, I didn't want to release it by itself because I knew it needed some more
      work before I did that.
      Recently, I delved back into it after several years and am now releasing version 2,
      which was mostly rewritten over the past several weeks.
      What does it do? It retrieves the properties and values pertaining to your computer,
      operating system and devices. All you need is a valid Win32 Class Name.
      So, here it is -- let me know if you have any issues!
      Updated to v2.25, January 14, 2017
      Download: _WMI_InstancesOf_v2.25.au3
       
    • TMelanson
      By TMelanson
      Hi folks,
      I'm hoping someone can help me out here.
      Background:
      I have the need to run a program with admin credentials (#RequireAdmin), and then get the SID of the locally logged in account. Not the admin account.
      If you look at the attached script, Line 16 uses the @UserName variable and returns the SID for the admin account I used to launch the app.
      I want to use line 17 which passes the result of the _GetUsername function which is a username using  ($UserName) to the WMIService.
      I'm getting " The requested action with this object has failed.:" error message.
      Any suggestions would be appreciated!
       
      test1.au3