65.9K
CodeProject is changing. Read more.
Home

Wmi2Dataset

starIconstarIconstarIconstarIconemptyStarIcon

4.00/5 (4 votes)

Nov 17, 2007

CPOL

2 min read

viewsIcon

26223

downloadIcon

700

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