Hey, Scripting Guy! How Do I Find the Names of WMI Classes?

Hey, Scripting Guy! How Do I Find the Names of WMI Classes?

  • Comments 1
  • Likes

Hey, Scripting Guy! Question

I was looking through some of the scripts in the Script Repository that use WMI, and they seem to make sense. In fact, they seem annoyingly repetitive. That is not my question. My question is how in the heck do you know which WMI class to use, or which properties are available in the WMI classes. I mean, that is the killer problem, now isn't it. It is easy after you know the WMI class name and the properties. Most of the time I have to search live.com to find a script that does what I want to do. I would like to try writing a script, but I need to know the class name first.

- MJ

SpacerHey, Scripting Guy! Answer

Hi MJ,

Sure it makes sense. You just need to know the secret word that will get you what you want from WMI. Kind of like going into a restaurant in Sydney, Australia. (Can you tell I love the place?) If you are an American, you need to know that if you want French fries, you need to ask for chips. If you want American potato chips, you need to ask for crisps. After you learn the secret words, it is not hard to know how to use them. Luckily we have an Australian-to-American translation dictionary. Oh wait; we do not have one after all. They do exist on the Internet; I will leave it up to you find one if you need such a tool. What we do have are some tools that will help with finding the names of WMI classes.

This week we will be looking at working with WMI from within Windows PowerShell. In honor of this week, we have created a new WMI Scripting Hub that pulls together a number of useful articles and tools from all over the Script Center into a single location. Because the articles will be using Windows PowerShell, you may also be interested in the Windows PowerShell Scripting Hub. You will find information there that will tell you how to download and install Windows PowerShell.

One tool you can use to search for WMI classes is the Scriptomatic (not to be confused with the Bassomatic). The Scriptomatic generates VBScript code and is written as an HTA. On Windows Vista there could be some issues when attempting to run it. These issues are discussed here in this Quick-Hits Friday article (near the bottom of the page). You might be wondering, dude, how is a tool that generates VBScript code going to help me write WMI in Windows PowerShell? And you would be perfectly within your rights to ask. When you open the Scriptomatic and click the drop-down list under WMI Class, you are presented with what you see in the image following this paragraph. The list is scrollable, and it displays all the dynamic WMI classes from the Root\Cimv2 WMI namespace. There are nearly 500 dynamic classes in this namespace and most of them can be used by IT pros.

Image of the WMI Class drop-down list

 

One way to find WMI classes is to simply use the Scriptomatic. Click the drop-down button under the WMI Class label. From the list seen above, if I were interested in information about the Bus on my computer, I would examine the Win32_Bus WMI class. But what properties are available? Using the Scriptomatic, I select Win32_Bus from the drop-down list, and yowza a script is created as seen in the image following this paragraph. Because the script is so long, much of the code has scrolled off the screen, but you can see that a number of properties such BusNum and BusType are available from the class.

Image of a script created by Scriptomatic

 

If you would like to see if those properties display data worth keeping, you can click Run, and it will run the VBScript. As seen in the image following this paragraph, the script returns a little bit of information, but all of the properties do not display data.

Image of not all properties displaying data

 

So you can see that by using the Scriptomatic, you can browse WMI classes, view their properties, and sample the data returned by the class. You could then query the class by using Windows PowerShell if you wish. These types of WMI queries were discussed in yesterday’s “Hey, Scripting Guy!” article.To query the Win32_Bus WMI class, for example, you could use the Windows PowerShell command seen here:

Get-WmiObject –Class Win32_Bus

If you would like to skip the middle man and avoid using VBScript, you can use the Windows PowerShell Scriptomatic. This program provides similar capabilities as the Scriptomatic, but it writes the code in Windows PowerShell, which allows you to directly use the code in a Windows PowerShell script once you have found the WMI class in which you were interested.

Both of these Scriptomatic tools are designed around the idea of scrolling through WMI class names and then generating code to query the class. But what could we do from within the Windows PowerShell console? What can I do using Windows PowerShell to obtain a listing of WMI class names? Take a look at the code shown here:

Get-WmiObject –List

If you want to see all the WMI classes in a different namespace, such as the root\wmi namespace, you only need to use the –namespace parameter:

Get-WmiObject -List -Namespace root\wmi

The output from the above command is seen here:

Image of the output of the command

 

We can then scroll up and down to our hearts content, just like we can do in the Scriptomatics. But because we are using Windows PowerShell, we also have superior text processing capabilities. This means we are not reduced to merely scrolling through a long list of WMI class names. Suppose you are interested in obtaining some WMI information related to the disk subsystem. It would make sense that the WMI class names just might have the word “disk” in their name. We can use the Where-Object cmdlet to filter each WMI class’s name property to see if we can find the word “disk” anywhere within the name. The code to do this is seen here:

Get-WmiObject -List | Where-Object { $_.name -match 'disk'}

We have now greatly reduced the number of entries that are returned. This is seen here:

Image of a greatly reduced number of entries

 

The command above can be written shorter if you wish. Gwmi is the alias for the Get-WmiObject cmdlet. There is only one parameter that begins with the letter l, so we can use –l. We pipeline the results to the Where-Object cmdlet. There is an alias, ?, for the Where-Object cmdlet. The remainder of the code is the same. So if you do not like to type, you can use a very terse (if obscure—you can make all your UNIX friends jealous!) syntax for Windows PowerShell. This short command is seen here:

gwmi -l | ? { $_.name -match 'disk'}

Well, MJ, hope this will give you some insight into identifying and searching for WMI class names. This is the second day of WMI week, and we have not written a Windows PowerShell WMI script yet. The week is still young, so we may break down and write one. Then again, we might not. Tune in tomorrow and find out (or check us out on http://www.twitter.com/ScriptingGuys, where we might spill the beans beforehand.) Until then, take care.

 

Ed Wilson and Craig Liebendorfer, Scripting Guys

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • When I double click on the ScriptomaticV2.hta, I get errors like ( Line 177 Char: 4 Error: could not complete the operation due to error 80041003 and Line: 1357 Char: 4 Error: Object required: 'NamespacePulldown)