Click here to Skip to main content
Click here to Skip to main content

Wmi2Dataset

, 17 Nov 2007
Rate this:
Please Sign up or sign in to vote.
Make Queries over WMI as in the Query Analyzer
Screenshot - WMI2Dataset

Introduction

Hello there. This is my second article on CodeProject. I hope you find it useful. This is a utility designed for obtaining information from a computer through the WMI (Windows Management Instrumentation) technology.

Background

There are a lot of references for using WMI in MSDN, however, I was wondering if there was a way of querying the information system in a way that was convenient for me. While looking at the syntax of obtaining the information (almost the same as SQL), I wished there was a Query Analyzer or something like that.... I searched but did not find one, so I began this little project and voila!

Using the Code

The core of the code in a nutshell is as follows:

  1. Create a ManagementScope object
  2. Create a ObjectQuery object with the user's query
  3. Create a ManagementObjectSearcher object with the query and the scope
  4. Obtain the result and store it in a ManagementObjectCollection
  5. Create a dataTable; generate structure from the result
  6. Fill the dataTable
public static DataTable QueryWmiADataTable(string query, string nombreTabla) {
    ConnectionOptions oConn = new ConnectionOptions();
    //oConn.Username = "DOMAIN\\USER";
    //oConn.Password = "";
    ManagementScope msAlcance = new ManagementScope("\\\\localhost", oConn);

    ObjectQuery oqQuery = new ObjectQuery(query);
    ManagementObjectSearcher mosBuscador = 
        new ManagementObjectSearcher(msAlcance, oqQuery);
    ManagementObjectCollection moColColeccion = mosBuscador.Get();

    DataTable dt = new DataTable(nombreTabla);
    foreach (ManagementObject obj in moColColeccion) {
        if (dt.Columns.Count == 0) {
            IEnumerator ie = obj.Properties.GetEnumerator();
            while (ie.MoveNext()) {
                PropertyData p = ie.Current as PropertyData;
                if (p != null) dt.Columns.Add(p.Name, TipoCIMATipo(p.Type));
                // Realizado por Jhonny D. Cano  jhonnycano at hotmail.com
            }
        }

        DataRow dr = dt.NewRow();
        foreach (DataColumn dc in dt.Columns) {
            object valor;
            if (dc.DataType.Equals(typeof(DateTime))) {
                valor = ToDateTime(obj[dc.ColumnName]);
            } else {
                valor = (obj[dc.ColumnName]);
            }
            dr[dc] = valor ?? DBNull.Value;
        }
        dt.Rows.Add(dr);
    }

    return dt;
}

The program submits the query with the F5 key, creating a DataTable with the name in the TextBox right up from the results grid. Note that the WMI works with different data types than C#, so in some cases (DateTime) it is necessary to convert the result for storing in the DataTable. In the code, there is the conversion function obtained from somewhere on the Internet. I also provided some sample queries at the right of the window. With double click, they will pass to the query text area.

You can save the query in a Text file, and the results in an XML format, it could be adapted for example for centralizing the hardware information of a network.

Another utility added to the application is the object visualizer for viewing the field returned when this is an array (For example in Win32_NetworkAdapterConfiguration).

Points of Interest

It has been very useful for me to develop this tool because it helped me understand the WMI classes much better. However, this is an extensive field, so I would ask you to research more on this topic.

History

  • 15-11-2007: First Release

License

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

About the Author

canozurdo
Web Developer
Colombia Colombia
I am Jhonny D Cano. I'm from Colombia, i'm professional in systems engineering. I worked since 1998 in Visual Basic and C++, then in 2.003 I jumped out to .Net development platform (VB.Net) and then i started with C#. I have a solid background in Windows Forms and in my current job I am working with ASP.Net and Web Services over Oracle and SQL Server. Alternatively I got the SUN Certification "Java Certified Programmer" and currently i'm in the process of 70-356's certification .
Follow on   Twitter

Comments and Discussions

 
GeneralPowerShell PinmemberEvyatar Ben-Shitrit18-Nov-07 5:10 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 17 Nov 2007
Article Copyright 2007 by canozurdo
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid