Scenario

You need to dump all the properties in a collection of WMI objects, but you don't want to manually use WriteLine() for each property because it's dull work.

Here is a link to code sample you want to avoid is : http://www.microsoft.com/technet/scriptcenter/scripts/os/services/ossvvb08.mspx

What you want

A function that takes collection of WMI objects and prints whatever properties are available.

Using Reflection to get what we want

In a previous post, I wrote about using Mgmtclassgen.exe to generate strongly typed WMI classes. Combine that with System.Reflection and you have an easy way to dump any collection of WMI objects.

The example below gets a list of WMI objects for the WIN32_Service class. The Service class is output from Mgmtclassgen.exe . Then it collects information about the properties for that wrapper class. Using that information it dynamically retrieves and prints the values for each WIN32_object via the method DumpProperties().

The DumpProperties() method will work with any of the other WMI classes generated my Mgmtclassgen.exe.

//-------------------------


using System;
using System.Collections.Generic;
using System.Text;
using con = System.Console;

namespace wmi_enum_with_reflection
{
    class Program
    {
        static void DumpProperties(object the_object, System.Reflection.PropertyInfo [] properties)
        {
            con.WriteLine("");
            con.WriteLine("{0} -----------------",the_object.GetType().Name);
            foreach (System.Reflection.PropertyInfo property in properties)
            {
                Type prop_type = property.PropertyType;
                string val = "";
                if ((prop_type == typeof(int)) 
                     || (prop_type == typeof(string))
                     || (prop_type == typeof(bool))
                     || (prop_type == typeof(float))
                     || (prop_type == typeof(double))
                     || (prop_type == typeof(UInt32))
                     || (prop_type == typeof(UInt64))
                     || (prop_type == typeof(UInt32))
                     || (prop_type == typeof(UInt16))
                     || (prop_type == typeof(System.DateTime))) { object obj_val = property.GetValue(the_object, null); if (obj_val == null) { val = "null"; } else { val = string.Format( "\"{0}\"",obj_val.ToString() ); } } else { val = "object=" + prop_type.ToString() + ""; } con.WriteLine(" {0} = {1}", property.Name, val); } } static void Main(string[] args) { Type service_type = typeof(ROOT.CIMV2.win32.Service); System.Reflection.PropertyInfo [] properties = service_type.GetProperties (); foreach (ROOT.CIMV2.win32.Service service in ROOT.CIMV2.win32.Service.GetInstances() ) { DumpProperties(service, properties); } } } } /--------------

An example of the output for one of the objects is here:

 

    Service -----------------
      OriginatingNamespace = "root\CimV2"
      ManagementClassName = "Win32_Service"
      SystemProperties = object=ROOT.CIMV2.win32.Service+ManagementSystemProperties
      LateBoundObject = object=System.Management.ManagementBaseObject
      Scope = object=System.Management.ManagementScope
      AutoCommit = "True"
      Path = object=System.Management.ManagementPath
      StaticScope = object=System.Management.ManagementScope
      IsAcceptPauseNull = "False"
      AcceptPause = "False"
      IsAcceptStopNull = "False"
      AcceptStop = "True"
      Caption = "Application Layer Gateway Service"
      IsCheckPointNull = "False"
      CheckPoint = "0"
      CreationClassName = "Win32_Service"
      Description = "Provides support for 3rd party protocol plug-ins for Internet Connection Sharing and the Windows Firewall."
      IsDesktopInteractNull = "False"
      DesktopInteract = "False"
      DisplayName = "Application Layer Gateway Service"
      ErrorControl = "Normal"
      IsExitCodeNull = "False"
      ExitCode = "0"
      IsInstallDateNull = "True"
      InstallDate = "1/1/0001 12:00:00 AM"
      Name = "ALG"
      PathName = "C:\WINDOWS\System32\alg.exe"
      IsProcessIdNull = "False"
      ProcessId = "368"
      IsServiceSpecificExitCodeNull = "False"
      ServiceSpecificExitCode = "0"
      ServiceType = "Own Process"
      IsStartedNull = "False"
      Started = "True"
      StartMode = "Manual"
      StartName = "NT AUTHORITY\LocalService"
      State = "Running"
      Status = "OK"
      SystemCreationClassName = "Win32_ComputerSystem"
      SystemName = "PIXEL7"
      IsTagIdNull = "False"
      TagId = "0"
      IsWaitHintNull = "False"
      WaitHint = "0"
      Site = object=System.ComponentModel.ISite
      Container = object=System.ComponentModel.IContainer
    

 

 

Wasn't that much easier?