Jump to content
Sign in to follow this  
Bilgus

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

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

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

I like having a little easily digestible info that shows a few point I actually made this for a seeker but I put this here for the next one(s)

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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By shital
      #Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_UseX64=y #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** RunAs(test", @ComputerName, "testinng",2,"wmic product where ""name= '%notepadexamples%'"" call uninstall", @SystemDir & "\wbem", @SW_MAXIMIZE)  it not working 
       
    • By Colduction
      Hi AutoIt Programmers!

      I've seen a code that gives Name of Group by writing it's group's SID, but this one response very slower than Windows Command-Line WMIC
       
      Func _GetLocalGroupName($sSID = 'S-1-5-18') $objWMIService = ObjGet ("winmgmts:\\" & @ComputerName & "\root\cimv2") $colItems = $objWMIService.ExecQuery('SELECT Name FROM Win32_Group where SID="' & $sSID & '"') For $GroupNames in $colItems MsgBox (0,"",$GroupNames.Name) ExitLoop Next EndFunc I don't want call and use any third-party programs even CMD, i just want use from Windows API, netapi32.dll or AutoIt Functions (Standalone).
      Do you have any idea to improve speed/performance of this code? I'll happy of your comments
       
       
       
      ====================== SOLOUTION by @Subz ======================
       
    • By jresine
      Hello, is it possible to know via a script or command, to have the percentage of disk usage of a process? thank you in advance.
      ps: see image

    • By Dwalfware
      HI
      Another head scratched
      Anyone solve how to get the IPV6 from WMI?
      I followed the usual SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionID='" & GUICtrlRead($cboAdapters) & "'
      get the index 
      port it to 
      Local $query = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE Index = " & _GetSelectedNIC(), "WQL", 0x30 )
      All i can see is IPv4 addresses
      My question is is there another place the IPv6 information is hiding? I configured my LAN with IPV6 and my wireless with IPv4 so I can see what shows.
      Thanks in Advance.
      PS. I did try google
       
    • By Dwalfware
      Hi Folks
      Can not get my head around this one.
      This function gets the DNS object s
      If you have two DNS entries fine. but if you have only one! Trouble
      $objItem.DNSServerSearchOrder[0] = Primary DNS
      $objItem.DNSServerSearchOrder[1] = Secondry DNS not always there. I need a way to return 0 if not there or the IP if its there.
      Or is there a way to tell how many objects there are in the "[?]"
      I'm terrible at WMI errors, thanks for help in advance.
      Func _GETcurrentNICinfo2() $oErrors = ObjEvent("AutoIt.Error", "Error_Handle") Local $DnsSecondry Local $objWMIService = ObjGet( "winmgmts:\\" & @ComputerName & "\root\CIMV2" ) Local $query = $objWMIService.ExecQuery("SELECT DNSServerSearchOrder FROM Win32_NetworkAdapterConfiguration WHERE Index = " & _GetSelectedNIC(), "WQL", 0x30 ) If @error Then Return 0 If NOT IsObj($query) Then Return 0 Else For $objItem In $query Return $objItem.DNSServerSearchOrder[1] next Endif EndFunc  
×
×
  • Create New...