Click here to Skip to main content
15,887,746 members
Articles / Desktop Programming / Win32

Get all data WMI has to offer

Rate me:
Please Sign up or sign in to vote.
3.88/5 (7 votes)
5 Oct 2009CPOL1 min read 36.2K   8   6
This simple function will programatically enumerate all properties and values in any WMI class.

Introduction

After looking at dozens of simple scripts and apps that each pull 5-6 fields of information from one WMI class, I decided it would be so much easier to just pull everything into a DataTable and work from there. This simple function exposes all of the properties and values on any given WMI class and returns a DataTable with the results.

The code

First off, you'll need to use these libraries:

VB
Imports System.Data
Imports System.Management
Imports Microsoft.Win32

Next up, create an empty DataTable and a ManagementObjectSearcher to be the base of our query to the WMI service.

VB
Dim dt As New DataTable()

Dim searcher As New ManagementObjectSearcher( _
    "root\CIMV2", _ "SELECT * FROM " & wmiClass)
    'where wmiClass is the input variable

In order to get all of the property names, you have to grab the first ManagementObject in the searcher. Then, simply loop through each of the property names in the object and add a column to the table for each property.

VB
For Each queryObj As ManagementObject In searcher.Get()
    For Each item As PropertyData In queryObj.Properties()
        Try
            dt.Columns.Add(item.Name)
        Catch ex As Exception
        End Try
    Next
    Exit For
Next

Then, loop through each of object in the searcher, and add the values for each property of each object to the data table:

VB
For Each queryObj As ManagementObject In searcher.Get()
    Dim dr As DataRow = dt.NewRow
    For Each item As PropertyData In queryObj.Properties()
        Try
            dr(item.Name) = item.Value
        Catch ex As Exception
        End Try
    Next
    dt.Rows.Add(dr)
Next

That's it! Now, you have a DataTable (dt) with all of the properties and values of the given WMI class. Try setting the wmiClass variable to any of the following:

  • Win32_OperatingSystem
  • Win32_OnBoardDevice (hardware devices)
  • Win32_Process (current processes)
  • Win32_Printer (printers)
  • Win32_Product (installed software)
  • Win32_QuickFixEngineering (installed patches)

Bind dt to a GridView and see the results. If you want to get really fancy, you can also use a DropDownList to set the wmiClass variable and easily see tons of data about your system. Microsoft has a simple reference for many of the WMI tasks here: http://msdn.microsoft.com/en-us/library/aa394585(VS.85).aspx.

Enjoy!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Systems Engineer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionRE: Get all data WMI has to offer Pin
LJWheeler23-Sep-15 4:21
LJWheeler23-Sep-15 4:21 
QuestionSort / Filter !! Pin
jithusree30-Dec-12 20:00
jithusree30-Dec-12 20:00 
Generalwbemtest.exe Pin
Thrash50527-Dec-12 7:48
Thrash50527-Dec-12 7:48 
GeneralWMI Data on different versions of Windows Pin
Jesse Fatherree16-Mar-10 6:19
Jesse Fatherree16-Mar-10 6:19 
Just a side note on why this method is really handy-

The property names can sometimes differ in different versions of Windows. So where something might be called "Name" in Windows XP, it's called "Hostname" in Windows Server 2008. So if you're trying to store this data in a database, it's something you'll need to be aware of. You may need some field mapping.

Also, in certain releases of Windows Server do not have all libraries installed by default. If you are getting a "Class not found" error, this is usually the case. For example, the Win32_Product class is missing on *some* Server 2003 R2 installations. To install it, go to Add/Remove Programs -> Windows Components -> Management and Monitoring Tools and enable the WMI Windows Installer Provider.
GeneralMy vote of 1 Pin
Qaiser_Iftikhar5-Oct-09 7:47
Qaiser_Iftikhar5-Oct-09 7:47 
GeneralRe: My vote of 1 Pin
Jesse Fatherree12-Mar-10 3:51
Jesse Fatherree12-Mar-10 3:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.