Use a PowerShell Function to Find Specific WMI Classes

Use a PowerShell Function to Find Specific WMI Classes

  • Comments 1
  • Likes

Summary: Use a Windows PowerShell function to find WMI classes with specific qualifiers.

 

Microsoft Scripting Guy Ed Wilson here. In Thursday’s article, I talked about using the Set-WmiInstance cmdlet to work with WMI classes. One of the parameters, the class parameter, works with WMI singleton objects. Now, it is certainly possible to use WBEMTest to find singleton WMI classes. Such a WMI class is shown in the following figure in the WBEMTest utility.

Image of a WMI singleton class

But with 1,085 WMI classes in Root\Cimv2, it is faster and more fun to use a WMI schema query. WMI schema queries are mentioned on MSDN, but there are no Windows PowerShell examples. I decided I needed to write a Windows PowerShell function that would query the schema to find the singleton classes for which I was looking. In addition, there are other class qualifiers I was interested in seeing as well. For example, there is a supportsupdate qualifier that lets me know that I can use that class to make modifications to a computer. There are other qualifiers that are even more important: abstract and dynamic. As an IT pro, I want to query dynamic WMI classes, and not the abstracts. For ease of use, I uploaded the script to the Scripting Guys Script Repository.

I ended up writing a function I could use to find classes with specific qualifiers. As shown in the following figure, there are a few singleton WMI classes. I opened the function in the Windows PowerShell ISE, ran the script once to load the function into memory, and then I went to the command pane and typed the following command:

Get-WMIClassesWithQualifiers -qualifier singleton

The command and associated output are shown in the following figure.

Image of command and associated output

A WMI schema query queries the meta_class WMI class. It uses the isa keyword to specify from which WMI class I want to return the schema information. That part is rather simple. The difficult part was getting the quotation marks placed in the right position to enable automatic querying. Here is the query line I derived:

$query = "select * from meta_class where __this isa ""$($class.name)"" "

I am interested in the qualifiers; therefore, I choose only the name of the WMI class and the qualifiers. This line appears is shown here:

$a = gwmi -Query $query -Namespace $namespace |

  select -Property __class, qualifiers

If the qualifiers contain the qualifier I am looking for, I return the WMI class name:

if($a.qualifiers | % { $_ | ? { $_.name -match "$qualifier" }})

    { $a.__class }

 

The core portion of the script, with the aliases removed is shown here:

Param([string]$qualifier = "dynamic",

  [string]$namespace = "root\cimv2")

 $classes = Get-WmiObject -list -namespace $namespace

 foreach($class in $classes)

 {

  $query = "Select * from meta_class where __this isa ""$($class.name)"" "

  $a = Get-WmiObject -Query $query -Namespace $namespace |

  Select-Object -Property __class, qualifiers

   if($a.qualifiers | ForEach-Object { $_ | Where-Object { $_.name -match "$qualifier" }})

    { $a.__class }

  } #end foreach $class

 

Well, that is about it for today. I hope you enjoy the function, and have an awesome weekend.

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

 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Hi Ed,

    this is quite interesting, if you have to look for WMI clsses with special properties.

    But I'm afraid there is hardly any more use for querying the meta classes, than that, what you presented here.

    So it is really a special purpose area, but it's good to know how if you have to ...!

    Klaus.