Search code examples
vbscriptwmi

How to list all WMI classes having methods using VBScript?


Using VBScript, how can I list all WMI classes that have methods?


Solution

  • Run a SELECT schema query to get a list of all classes in a namespace, and then check each class's Methods_.Count:

    strComputer  = "."
    strNamespace = "root\cimv2"
    
    Set oWMI = GetObject("winmgmts:\\" & strComputer & "\" & strNamespace)
    Set colClasses = oWMI.ExecQuery("SELECT * FROM meta_class") 
    
    For Each oClass in colClasses
      If oClass.Methods_.Count > 0 Then
        WScript.Echo oClass.Path_.Class
      End If
    Next
    

    You may want to limit the results to dynamic and static classes only, like WMI Code Creator does. To do this, add an additional check for the corresponding class qualifiers.

    ...
    For Each oClass in colClasses
    
      For Each oQualifier In oClass.Qualifiers_
        strQualName = LCase(oQualifier.Name)
    
        If strQualName = "dynamic" OR strQualName = "static" Then
          If oClass.Methods_.Count > 0 Then
            WScript.Echo oClass.Path_.Class
          End If
        End If
    
      Next
    Next
    

    I also suggest that you read the WMI Scripting Primer: Part 2 article. It explains the WMI concepts and infrastructure in detail and with examples, and may already hold answers to your future questions. :)