none
WMI Namespace RRS feed

  • Question

  • This is code I found to enumerate the WMI Namespaces:
    Module Module1
        Sub EnumNameSpaces(ByVal strNameSpace)

            Console.WriteLine(strNameSpace)

            Dim strComputer As String = "."
            Dim objWMIService As Object = GetObject("winmgmts:\\" & strComputer & "\" & strNameSpace)
            Dim colNameSpaces As Object = objWMIService.InstancesOf("__NAMESPACE")
            For Each objNameSpace In colNameSpaces
                Call EnumNameSpaces(strNameSpace & "\" & objNameSpace.Name)
            Next

        End Sub

        Sub Main()
            Call EnumNameSpaces("root")
            Console.ReadLine()
        End Sub

    End Module

    The Output is (must run as admin FYI) 

    root
    root\subscription
    root\subscription\ms_409
    root\DEFAULT
    root\DEFAULT\ms_409
    root\CIMV2
    root\CIMV2\Security
    root\CIMV2\Security\MicrosoftTpm
    root\CIMV2\Security\MicrosoftVolumeEncryption
    root\CIMV2\power
    root\CIMV2\power\ms_409
    root\CIMV2\ms_409
    root\CIMV2\TerminalServices
    root\CIMV2\TerminalServices\ms_409
    root\CIMV2\Applications
    root\CIMV2\Applications\WindowsParentalControls
    root\CIMV2\Applications\Games
    root\CIMV2\Applications\Games\ms_409
    root\Cli
    root\Cli\MS_409
    root\nap
    root\SECURITY
    root\SecurityCenter2
    root\RSOP
    root\RSOP\User
    root\RSOP\User\S_1_5_21_3149542145_3322839065_4058237693_500
    root\RSOP\User\S_1_5_21_1860128955_2317930214_598928839_1000
    root\RSOP\User\S_1_5_21_1860128955_2317930214_598928839_500
    root\RSOP\User\ms_409
    root\RSOP\Computer
    root\RSOP\Computer\ms_409
    root\WMI
    root\WMI\ms_409
    root\directory
    root\directory\LDAP
    root\directory\LDAP\ms_409
    root\Policy
    root\Policy\ms_409
    root\Interop
    root\Interop\ms_409
    root\ServiceModel
    root\SecurityCenter
    root\MSAPPS12
    root\Microsoft
    root\Microsoft\HomeNet
    root\aspnet

     

    As an example - Under root\SecurityCenter2 is this (output from another program)
    code:
    Dim AVSearcher As New ManagementObjectSearcher("root\SecurityCenter2", "SELECT * FROM AntivirusProduct")
    For Each queryObj As ManagementObject In AVSearcher.Get()
    Console.WriteLine( Replace(queryObj.GetText(TextFormat.Mof), vbLf, vbCrLf) & vbCrLf)
    Next

    Actual Output:

    instance of AntiVirusProduct
    {
     displayName = "AVG Internet Security 2011";
     instanceGuid = "{5A2746B1-DEE9-F85A-FBCD-ADB11639C6E3}";
     pathToSignedProductExe = "C:\\Program Files\\AVG\\AVG10\\avgwsc.exe";
     pathToSignedReportingExe = "C:\\Program Files\\AVG\\AVG10\\avgwdsvc.exe";
     productState = 266240;
    };

    I know that "subitem"(Antivirus Product) from research I did, what I am trying to do is fully enumerate all possible query items under each NameSpace that is found in the example code above.
    For instance there is also a subitem "FirewallProduct" under SecurityCenter2

    I hope this makes sense...........

     

    Friday, August 12, 2011 6:34 PM

Answers

  • well, since you have already enumerated the namespaces, now you need to enumerate the classes available in each namespace. this article shows how to do it in powershell with Get-WmiObject -List. http://technet.microsoft.com/en-us/library/dd315379.aspx

    This article shows how to do it in pure vbs: http://technet.microsoft.com/en-us/magazine/2008.11.heyscriptingguy.aspx (scroll down a little).

    • Marked as answer by Devon_Nullman Saturday, August 13, 2011 3:52 AM
    Friday, August 12, 2011 7:07 PM
  • You can use the WMI Administrative tools to browse the WMI tree. Enumerating the whole lot from root is not very useful.

    Add a reference to System.Management and use the classes there to enumerate the tree, it's easier than late bound COM code.

    If you really want to enumerate the whole tree then you can. Quick code...

    Option Strict On
    Option Explicit On
    
    Imports System.Management
    Imports System.Text
    
    Public Class Form1
    
      Private WithEvents tv1 As New TreeView With {.Dock = DockStyle.Fill}
      Private enumOptions As New EnumerationOptions With {.EnumerateDeep = True}
    
      Sub New()
        ' This call is required by the designer.
        InitializeComponent()
    
        ' Add any initialization after the InitializeComponent() call.
        Me.Controls.Add(tv1)
        BuildTree()
      End Sub
    
      Private Sub BuildTree()
        Dim root As String = "\\.\root"
        Dim n As New TreeNode(root)
        n.Tag = root
        AddSubNamespaceNodes(n)
        tv1.Nodes.Add(n)
      End Sub
    
      Private Sub AddSubNamespaceNodes(ByVal parent As TreeNode)
        Dim path As String = DirectCast(parent.Tag, String)
        Using rootClass As New ManagementClass(path, "__NAMESPACE", New ObjectGetOptions())
          Using moc1 As ManagementObjectCollection = rootClass.GetInstances()
            For Each mo1 As ManagementObject In moc1
              Dim name As String = CStr(mo1.Item("Name"))
              AddNameSpaceNode(parent, path, name)
              mo1.Dispose()
            Next
          End Using
        End Using
        For Each n As TreeNode In parent.Nodes
          AddSubNamespaceNodes(n)
        Next
        For Each n As TreeNode In parent.Nodes
          AddSubClassNodes(n)
        Next
      End Sub
    
      Private Sub AddNameSpaceNode(ByVal parent As TreeNode, ByVal parentPath As String, ByVal name As String)
        Dim path As String = IO.Path.Combine(parentPath, name)
        Dim n As New TreeNode(name)
        n.Tag = path
        parent.Nodes.Add(n)
      End Sub
    
      Private Sub AddSubClassNodes(ByVal parent As TreeNode)
        Dim path As String = DirectCast(parent.Tag, String)
        Dim nodes As New Dictionary(Of String, TreeNode)
        Using subClass As New ManagementClass(path)
          Using moc2 As ManagementObjectCollection = subClass.GetSubclasses(enumOptions)
            For Each mo2 In moc2
              Dim className As String = CStr(mo2("__CLASS"))
              Dim superClass As String = TryCast(mo2("__SUPERCLASS"), String)
              Dim subNode As New WmiClassNode()
              subNode.Text = className
              subNode.Path = mo2.ClassPath.Path
              subNode.ForeColor = Color.Green
              subNode.Tag = superClass
              nodes.Add(className, subNode)
              If superClass IsNot Nothing Then
                If nodes.ContainsKey(superClass) Then
                  nodes(superClass).Nodes.Add(subNode)
                End If
              End If
            Next
          End Using
        End Using
        parent.Nodes.AddRange(nodes.Values.Where(Function(x) x.Tag Is Nothing).ToArray)
      End Sub
    
      Private Sub tv1_NodeMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles tv1.NodeMouseDoubleClick
        Dim classNode As WmiClassNode = TryCast(tv1.SelectedNode, WmiClassNode)
        If classNode IsNot Nothing Then
          ' more or less copied from msdn...
          Dim sb As New StringBuilder
          sb.AppendLine(classNode.Text)
          sb.AppendLine(classNode.Path)      
          Using wmiClass As New ManagementClass(classNode.Path)
            sb.AppendLine("Properties")
            sb.AppendLine("----------")
            sb.AppendLine()
            wmiClass.Options.UseAmendedQualifiers = True
            Dim properties As PropertyDataCollection = wmiClass.Properties
            For Each prop As PropertyData In properties
              sb.AppendLine(prop.Name)
              For Each q As QualifierData In prop.Qualifiers
                If (q.Name.Equals("Description")) Then
                  Dim qualifierValue As String
                  qualifierValue = CStr(wmiClass.GetPropertyQualifierValue(prop.Name, q.Name))
                  sb.AppendLine("(" & qualifierValue & ")")
                End If
              Next
            Next
            sb.AppendLine()
            sb.AppendLine("Methods")
            sb.AppendLine("-------")
            sb.AppendLine()
            Dim methods As MethodDataCollection = wmiClass.Methods
            For Each method As MethodData In methods
              sb.AppendLine(method.Name)
              For Each q As QualifierData In method.Qualifiers
                If (q.Name.Equals("Description")) Then
                  Dim qualifierValue As String
                  qualifierValue = CStr(wmiClass.GetPropertyQualifierValue(method.Name, q.Name))
                  sb.AppendLine("(" & qualifierValue & ")")
                End If
              Next
            Next
          End Using
          Using f As New Form
            Using tb As New TextBox
              tb.Dock = DockStyle.Fill
              tb.Text = sb.ToString
              tb.Multiline = True
              f.Controls.Add(tb)
              f.ShowDialog()
            End Using
          End Using
        End If    
      End Sub
    
    End Class
    
    Public Class WmiClassNode
      Inherits TreeNode
    
      Private m_path As String
      Public Property Path() As String
        Get
          Return m_path
        End Get
        Set(ByVal value As String)
          m_path = value
        End Set
      End Property
    
    End Class
    
    

    So it recurses through the namespaces, and for each one it gets a flat list of classes. It parses out the classes by looking at each one's superclass (thankfully they are presented in such order that you never get a subclass before a superclass) so that the classes get nested correctly. The classes store the wmi path in a property - so if you double click one it can find it again to get property and method data.

     

    • Marked as answer by Devon_Nullman Saturday, August 13, 2011 4:15 AM
    Friday, August 12, 2011 9:39 PM

All replies

  • well, since you have already enumerated the namespaces, now you need to enumerate the classes available in each namespace. this article shows how to do it in powershell with Get-WmiObject -List. http://technet.microsoft.com/en-us/library/dd315379.aspx

    This article shows how to do it in pure vbs: http://technet.microsoft.com/en-us/magazine/2008.11.heyscriptingguy.aspx (scroll down a little).

    • Marked as answer by Devon_Nullman Saturday, August 13, 2011 3:52 AM
    Friday, August 12, 2011 7:07 PM
  • You can use the WMI Administrative tools to browse the WMI tree. Enumerating the whole lot from root is not very useful.

    Add a reference to System.Management and use the classes there to enumerate the tree, it's easier than late bound COM code.

    If you really want to enumerate the whole tree then you can. Quick code...

    Option Strict On
    Option Explicit On
    
    Imports System.Management
    Imports System.Text
    
    Public Class Form1
    
      Private WithEvents tv1 As New TreeView With {.Dock = DockStyle.Fill}
      Private enumOptions As New EnumerationOptions With {.EnumerateDeep = True}
    
      Sub New()
        ' This call is required by the designer.
        InitializeComponent()
    
        ' Add any initialization after the InitializeComponent() call.
        Me.Controls.Add(tv1)
        BuildTree()
      End Sub
    
      Private Sub BuildTree()
        Dim root As String = "\\.\root"
        Dim n As New TreeNode(root)
        n.Tag = root
        AddSubNamespaceNodes(n)
        tv1.Nodes.Add(n)
      End Sub
    
      Private Sub AddSubNamespaceNodes(ByVal parent As TreeNode)
        Dim path As String = DirectCast(parent.Tag, String)
        Using rootClass As New ManagementClass(path, "__NAMESPACE", New ObjectGetOptions())
          Using moc1 As ManagementObjectCollection = rootClass.GetInstances()
            For Each mo1 As ManagementObject In moc1
              Dim name As String = CStr(mo1.Item("Name"))
              AddNameSpaceNode(parent, path, name)
              mo1.Dispose()
            Next
          End Using
        End Using
        For Each n As TreeNode In parent.Nodes
          AddSubNamespaceNodes(n)
        Next
        For Each n As TreeNode In parent.Nodes
          AddSubClassNodes(n)
        Next
      End Sub
    
      Private Sub AddNameSpaceNode(ByVal parent As TreeNode, ByVal parentPath As String, ByVal name As String)
        Dim path As String = IO.Path.Combine(parentPath, name)
        Dim n As New TreeNode(name)
        n.Tag = path
        parent.Nodes.Add(n)
      End Sub
    
      Private Sub AddSubClassNodes(ByVal parent As TreeNode)
        Dim path As String = DirectCast(parent.Tag, String)
        Dim nodes As New Dictionary(Of String, TreeNode)
        Using subClass As New ManagementClass(path)
          Using moc2 As ManagementObjectCollection = subClass.GetSubclasses(enumOptions)
            For Each mo2 In moc2
              Dim className As String = CStr(mo2("__CLASS"))
              Dim superClass As String = TryCast(mo2("__SUPERCLASS"), String)
              Dim subNode As New WmiClassNode()
              subNode.Text = className
              subNode.Path = mo2.ClassPath.Path
              subNode.ForeColor = Color.Green
              subNode.Tag = superClass
              nodes.Add(className, subNode)
              If superClass IsNot Nothing Then
                If nodes.ContainsKey(superClass) Then
                  nodes(superClass).Nodes.Add(subNode)
                End If
              End If
            Next
          End Using
        End Using
        parent.Nodes.AddRange(nodes.Values.Where(Function(x) x.Tag Is Nothing).ToArray)
      End Sub
    
      Private Sub tv1_NodeMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles tv1.NodeMouseDoubleClick
        Dim classNode As WmiClassNode = TryCast(tv1.SelectedNode, WmiClassNode)
        If classNode IsNot Nothing Then
          ' more or less copied from msdn...
          Dim sb As New StringBuilder
          sb.AppendLine(classNode.Text)
          sb.AppendLine(classNode.Path)      
          Using wmiClass As New ManagementClass(classNode.Path)
            sb.AppendLine("Properties")
            sb.AppendLine("----------")
            sb.AppendLine()
            wmiClass.Options.UseAmendedQualifiers = True
            Dim properties As PropertyDataCollection = wmiClass.Properties
            For Each prop As PropertyData In properties
              sb.AppendLine(prop.Name)
              For Each q As QualifierData In prop.Qualifiers
                If (q.Name.Equals("Description")) Then
                  Dim qualifierValue As String
                  qualifierValue = CStr(wmiClass.GetPropertyQualifierValue(prop.Name, q.Name))
                  sb.AppendLine("(" & qualifierValue & ")")
                End If
              Next
            Next
            sb.AppendLine()
            sb.AppendLine("Methods")
            sb.AppendLine("-------")
            sb.AppendLine()
            Dim methods As MethodDataCollection = wmiClass.Methods
            For Each method As MethodData In methods
              sb.AppendLine(method.Name)
              For Each q As QualifierData In method.Qualifiers
                If (q.Name.Equals("Description")) Then
                  Dim qualifierValue As String
                  qualifierValue = CStr(wmiClass.GetPropertyQualifierValue(method.Name, q.Name))
                  sb.AppendLine("(" & qualifierValue & ")")
                End If
              Next
            Next
          End Using
          Using f As New Form
            Using tb As New TextBox
              tb.Dock = DockStyle.Fill
              tb.Text = sb.ToString
              tb.Multiline = True
              f.Controls.Add(tb)
              f.ShowDialog()
            End Using
          End Using
        End If    
      End Sub
    
    End Class
    
    Public Class WmiClassNode
      Inherits TreeNode
    
      Private m_path As String
      Public Property Path() As String
        Get
          Return m_path
        End Get
        Set(ByVal value As String)
          m_path = value
        End Set
      End Property
    
    End Class
    
    

    So it recurses through the namespaces, and for each one it gets a flat list of classes. It parses out the classes by looking at each one's superclass (thankfully they are presented in such order that you never get a subclass before a superclass) so that the classes get nested correctly. The classes store the wmi path in a property - so if you double click one it can find it again to get property and method data.

     

    • Marked as answer by Devon_Nullman Saturday, August 13, 2011 4:15 AM
    Friday, August 12, 2011 9:39 PM
  • Thanks, I think :) - added the code and it has been churning out the entire detailed data for 10 minutes, well over 100 MB.

    Correction - now 150 MB - as they say, be careful what you wish for.

     

     

    Saturday, August 13, 2011 3:56 AM
  • Thanks - that is much nicer than my Console App.

     

    Saturday, August 13, 2011 4:15 AM