Finding all WMI class properties with .NET C#
January 8, 2017 1 Comment
In this post we saw how to enumerate all WMI – Windows Management Intrumentation – namespaces and classes. Then in this post we saw an example of querying the system to retrieve all local drives:
ObjectQuery objectQuery = new ObjectQuery("SELECT Size, Name FROM Win32_LogicalDisk where DriveType=3");
The properties that we’re after are like “Size” and “Name” of Win32_LogicalDisk. There’s a straightforward solution as we can select all properties in the object query. The following method will print all properties available in the WMI class, their types and values:
private static void PrintPropertiesOfWmiClass(string namespaceName, string wmiClassName) { ManagementPath managementPath = new ManagementPath(); managementPath.Path = namespaceName; ManagementScope managementScope = new ManagementScope(managementPath); ObjectQuery objectQuery = new ObjectQuery("SELECT * FROM " + wmiClassName); ManagementObjectSearcher objectSearcher = new ManagementObjectSearcher(managementScope, objectQuery); ManagementObjectCollection objectCollection = objectSearcher.Get(); foreach (ManagementObject managementObject in objectCollection) { PropertyDataCollection props = managementObject.Properties; foreach (PropertyData prop in props) { Console.WriteLine("Property name: {0}", prop.Name); Console.WriteLine("Property type: {0}", prop.Type); Console.WriteLine("Property value: {0}", prop.Value); } } }
You’ll need to run this with VS as an administrator. Also, there’s no authentication so we’ll use this code to investigate the class properties on the local machine. Otherwise see the posts referred to above for an example to read WMI objects from another machine on your network.
Let’s see what’s there for us in the cimv2/Win32_LocalTime class:
PrintPropertiesOfWmiClass("root\\cimv2", "Win32_LocalTime");
I got the following output:
Let’s see another one:
PrintPropertiesOfWmiClass("root\\cimv2", "Win32_BIOS");
Some interesting property values from the BIOS properties of my PC:
You can view all posts related to Diagnostics here.
Powershell way: (Get-WmiObject win32_LocalTime) | gm