Use PowerShell 3.0 and the CIM Cmdlets to Explore WMI Classes

Doctor Scripto

Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell 3.0 and the CIM cmdlets to explore WMI classes.

Microsoft Scripting Guy, Ed Wilson, is here. Today, the Scripting Wife and I are on the train from Dortmund to Geneva. Teresa was really smart and found tickets for us to tour the Particle Accelerator at Cern. This is a nice train ride, and in addition to being a great place to work, the scenery outside is wonderful as well.

While in Dortmund, we had time to spend the day with Windows PowerShell Guru Klaus Schulte. In addition to talking about Windows PowerShell, we walked around Dortmund taking pictures. Here is a photo of the Scripting Wife, Klaus, and his wife, Susanne, during our walk.

Photo of Teresa, Klaus, and his wife, Susanne

One of the cool things about Dortmund is they have one of the nicest burger places I have ever seen; not that we ate there, but it just looked cool, as shown here.

Photo of burger place

One of the problems with traveling on a train is how expensive wireless Internet is, and I just do not want to pay that much. This means I am limited to resources on my laptop. In the old days, I used to install the WMI SDK on my laptop so that I would have ready information about the classes. These days the WMI SDK is incorporated with the Windows Platform SDK (or maybe we call it something else), and this takes a whole lot of disk space that I really do not have. Luckily, I do not need to invest this much disk space to just find out things like writable properties or implemented methods of WMI classes. The Get-CIMClass cmdlet does an excellent job of exposing just the information I need.

Exploring WMI classes

Many WMI classes have writable properties. To see these, I need to look for the write qualifier. Well, I do not exactly have to do this—I can use the WbemTest utility to find this information. So, I type WbemTest in my Windows PowerShell console. When WbemTest opens, I have to click connect to connect to the root/cimv2 WMI namespace. Once this connects, I can now click Open Class and type Win32_Computersystem, for example. Now I have the Object Editor for Win32_ComputerSystem. I have to look at every single property individually to see the qualifiers. If a property does not possess the write qualifier, it is read-only, as shown here.

Image of Property Editor

Using the old Get-WmiObject cmdlets and piping the results to Get-Member is misleading because it reports all properties as Get;Set (but I know from WbemTest that the name property is read-only). This is shown here.

PS C:\> Get-WmiObject win32_computersystem | get-member -Name name

   TypeName: System.Management.ManagementObject#root\cimv2\Win32_ComputerSystem

 

Name MemberType Definition

—- ———- ———-

Name Property   string Name {get;set;}

 Using the CIM classes to find WMI Writable properties

The easy way to find WMI class information is to use the Get-CimClass cmdlet. For example, to see all of the writable properties from a WMI class, such as the Win32_ComputerSystem WMI class, I can use the following code.

Get-CimClass win32_computersystem | select -ExpandProperty cimclassproperties |

where qualifiers -match ‘write’ | Format-table name, qualifiers

The command and associated output is shown in the following image.

Image of command output

Using Get-CimClass to find WMI methods

In the same way that I can find writable WMI properties, I can also find implemented WMI methods. A WMI class may have five methods and only three of them might have the implemented qualifier. This is because all WMI classes inherit from other WMI classes that inherit from other WMI classes (and so on and so on). Therefore, an abstract may have a SetPowerState method, but the dynamic WMI class I want to use may not implement the method (indeed, I know of no WMI class that implements the SetPowerState method).

So, in the same way I looked for the write qualifier for cimclassproperties, I look for the implemented qualifier for the cimclassmethods property, as shown here.

Get-CimClass win32_computersystem | select -ExpandProperty cimclassmethods |

where qualifiers -match ‘implemented’ | Format-Table name, qualifiers

The code to look at the implemented methods of the Win32_ComputerSystem WMI class (along with the associated results) is shown here.

Image of command output

Well, the Scripting Wife just came back to our seats with tea and chocolate (I did not ask her to find a snack, she just did it because she is nice). I guess I need to put the laptop up for a bit. Join me tomorrow when I will talk about using Windows PowerShell and WMI to view or to set power plans. It’s cool.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy

0 comments

Discussion is closed.

Feedback usabilla icon