Friday, October 29, 2004

WMI and .NET

The Windows Management Instrumentation API is an implementation of WBEM, the Web-based Enterprise Management, a IETF-managed set of core standards for systems management.

The data model used is termed the CIM or Common Information Model. This includes models for Systems, Applications, Networks and Devices.

The .NET Framework provides a set of classes to use WMI. In the best traditions of .NET, it is very easy to use reflection to discover and execute operations on items represented by these WMI Classes. The WMI Classes are under the System.Management namespace.

The relevant classes are:

  • ManagementObject or ManagementClass: A specific management object or class

  • ManagementObjectSearcher: Returns a collection of ManagementObjects based on a filter from ManagementQuery

  • ManagementEventWatcher: WMI Event Handler class

  • ManagementQuery: Filter definition class



A couple of good blog entries on WMI are at "The Scripting Guys' Blog" where they look at listing a directory tree recursively using WMI

Here is a small program to illustrate accessing & using WMI objects:


using System;
using System.Management;

namespace WMITest
{
///
/// Use WMIClasses from .NET
///

class WMIClass
{
[STAThread]
static void Main(string[] args)
{
try
{
string[] WMIClasses = new string[10];
GetClasses(WMIClasses);
foreach(string WMIClass in WMIClasses)
{
if(WMIClass !=null)
{
ManagementObjectCollection mosColl=null;
mosColl = getObjects(WMIClass);
showProperties(mosColl,WMIClass);
}
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message+ex.StackTrace);
}
}

static void GetClasses(string[] WMIClasses)
{
int i=0;
WMIClasses[i++] = "Win32_Keyboard";
WMIClasses[i++] = "Win32_NetworkAdapterConfiguration";
WMIClasses[i++] = "Win32_PointingDevice";
WMIClasses[i++] = "Win32_MemoryArray";
WMIClasses[i++] = "Win32_MotherboardDevice";
WMIClasses[i++] = "Win32_PrinterConfiguration";
WMIClasses[i++] = "Win32_Environment";
WMIClasses[i++] = "Win32_Process";
WMIClasses[i++] = "Win32_PerfFormattedData_PerfOS_Cache";
WMIClasses[i++] = "Win32_PerfFormattedData_PerfOS_Memory";
WMIClasses[i++] = "Win32_PerfFormattedData_PerfOS_Processor";
WMIClasses[i++] = "Win32_PerfFormattedData_Tcpip_IP";
}

static ManagementObjectCollection getObjects(string WMIClass)
{
// 1. Define management scope
// 2. Define query in scope
// 3. Execute query and store results
// 4. Iterate through results
string query = "SELECT * FROM " + WMIClass;
ObjectQuery oq = new ObjectQuery(query);
ConnectionOptions co = new ConnectionOptions();
ManagementScope ms = new
ManagementScope(ManagementPath.DefaultPath, co);
ManagementObjectSearcher mosQuery = new
ManagementObjectSearcher(ms,oq);
ManagementObjectCollection mosColl = mosQuery.Get();
return mosColl;
}

static void showProperties(ManagementObjectCollection mosQueryColl,
string WMIClass)
{
Console.WriteLine(WMIClass + " Properties ");
Console.WriteLine("-----------------------------");
foreach(ManagementObject mo in mosQueryColl)
{
//Properties
foreach(PropertyData prop in mo.Properties)
Console.WriteLine("{0} - {1}", prop.Name, prop.Value);
Console.WriteLine("-----------------------------");
}
}
}
}