Jump to content

WMI Enumerate all CIMv2 Classes and show a Few Examples of enumerating properties


Bilgus
 Share

Recommended Posts

#include <Array.Au3>
;WMI EXAMPLE
Local $strComputer = "localhost"
Local $sClass = ""
Local $objClass

Local $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2")

; Display all available Classes in this object

_ArrayDisplay(Get_Classes($objWMIService), "AVAILABLE WMI CLASSES", "", 0, Default, "NAME") ;Split string at \n(@LF) place each into an array

$sClass = "Win32_BIOS" ;<-The Class I've choosen
;Lets Try out one of the classes
Local $objClass = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2:" & $sClass)

ConsoleWrite($sClass & @CRLF & "------------------------------------------" & @CRLF)

_ArrayDisplay(Split_Properties(Get_Properties($objClass)), $sClass & " Properties", "", 0, Default, "NAME|VALUE")

;No methods available for this one but...
ConsoleWrite("Methods:" & @CRLF & @CRLF)
Get_Methods($objClass)

;Lets Do it again for the network adapter Class
$sProperties = ""
$sClass = "Win32_NetworkAdapterConfiguration" ;<-The Class I've choosen
;Lets Try out one of the classes
$objClass = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2:" & $sClass)

ConsoleWrite($sClass & @CRLF & "------------------------------------------" & @CRLF)

_ArrayDisplay(Split_Properties(Get_Properties($objClass)), $sClass & " Properties", "", 0, Default, "NAME|VALUE")

ConsoleWrite("Methods:" & @CRLF & @CRLF)
Get_Methods($objClass)

Func Get_Classes($obj)
    ; Display all available Classes in this object
    If IsObj($obj) Then
        Local $sClasses = ""
        For $objClass In $obj.SubclassesOf() ;<--WMI Method
            $sClasses &= ($objClass.Path_.Class) & @LF ;Build a string seperated by \n(@LF)
        Next
    EndIf
    Return StringSplit($sClasses, @LF)
EndFunc   ;==>Get_Classes

Func Get_Methods($objClass)
    Local $sResults = ""
    If IsObj($objClass) Then
        For $objMethods In $objClass.Methods_
            ConsoleWrite(@TAB & $objMethods.Name & @CRLF)
            $sResults &= $objMethods.Name & @LF
        Next
    EndIf
    Return $sResults
EndFunc   ;==>Get_Methods

Func Get_Properties($objClass)
    Local $sProperties = ""
    If IsObj($objClass) Then
        For $objClassProp In $objClass.Properties_() ;<-Another WMI Method
            For $obj In $objWMIService.ExecQuery("Select * from " & $sClass) ;<-Another WMI Method
                $sProperties &= $objClassProp.Name & @CR
                $sProperties &= Parse_Value($obj.Properties_($objClassProp.Name).Value) ;Use the Properties_ Method to call our desired property
            Next
        Next
    EndIf
    Return $sProperties
EndFunc   ;==>Get_Properties

Func Parse_Value($vValue)

    Local $sRet = ""
    Switch StringLower(VarGetType($vValue))
        Case "keyword" ; Not really sure what this one is probably NULL
            $sRet = Number($vValue) = 0 ? "" : Number($vValue)
        Case "array"
            $sRet = _ArrayToString($vValue, ", ")
        Case "string", "bool", "int32", "int64", "double", "binary"
            $sRet = $vValue
        Case "pointer"
            $sRet = "[PTR]:" & $vValue
        Case Else
            $sRet = "[" & VarGetType($vValue) & "]: " & $vValue
    EndSwitch
    $sRet &= @LF
    Return $sRet
EndFunc   ;==>Parse_Value

Func Split_Properties($sProperties)
    Local $aDisp = StringSplit($sProperties, @LF) ;Split string at \n(@LF) place each into an array
    Local $aTmp
    If IsArray($aDisp) Then
        _ArrayColInsert($aDisp, 1)

        For $i = 1 To $aDisp[0][0] ; String Split stores Count in the first element by default
            $aTmp = StringSplit($aDisp[$i][0], @CR) ;Split string at \r(@CR) place each into an array
            If IsArray($aTmp) Then
                $aDisp[$i][0] = $aTmp[0] > 0 ? $aTmp[1] : "?" ;Check if this element exists if not make it "?"
                $aDisp[$i][1] = $aTmp[0] > 1 ? $aTmp[2] : " " ;Check if this element exists if not make it " "
            EndIf
        Next
    EndIf
    Return $aDisp
EndFunc   ;==>Split_Properties

 

Edited by Bilgus
Link to comment
Share on other sites

  • Moderators

Just curious, this is great at displaying the classes returned, but to interact with them a user would still need different code. Why not just use Scriptomatic?

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

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