{"id":12321,"date":"2011-10-22T00:01:00","date_gmt":"2011-10-22T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/10\/22\/use-a-powershell-function-to-find-specific-wmi-classes\/"},"modified":"2011-10-22T00:01:00","modified_gmt":"2011-10-22T00:01:00","slug":"use-a-powershell-function-to-find-specific-wmi-classes","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-a-powershell-function-to-find-specific-wmi-classes\/","title":{"rendered":"Use a PowerShell Function to Find Specific WMI Classes"},"content":{"rendered":"<p><strong>Summary:<\/strong> Use a Windows PowerShell function to find WMI classes with specific qualifiers.<\/p>\n<p>&nbsp;<\/p>\n<p>Microsoft Scripting Guy Ed Wilson here. In <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/10\/20\/use-the-set-wmiinstance-powershell-cmdlet-to-ease-configuration.aspx\">Thursday&rsquo;s article<\/a>, I talked about using the <b>Set-WmiInstance<\/b> cmdlet to work with WMI classes. One of the parameters, the <i>class <\/i>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.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5383.hsg-10-22-11-1.png\"><img decoding=\"async\" title=\"Image of a WMI singleton class\" alt=\"Image of a WMI singleton class\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5383.hsg-10-22-11-1.png\" \/><\/a><\/p>\n<p>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 <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/aa393278(v=vs.85).aspx\">mentioned on MSDN<\/a>, 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 <b>supportsupdate<\/b><i> <\/i>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: <b>abstract<\/b> and <b>dynamic<\/b>. As an IT pro, I want to query <i>dynamic <\/i>WMI classes, and not the abstracts. For ease of use, I uploaded the script to the <a href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/Get-WMI-Class-qualifiers-239970e7\">Scripting Guys Script Repository<\/a>.<\/p>\n<p>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 <b>singleton<\/b><i> <\/i>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:<\/p>\n<p style=\"padding-left: 30px\">Get-WMIClassesWithQualifiers -qualifier singleton<\/p>\n<p>The command and associated output are shown in the following figure.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7120.hsg-10-22-11-2.png\"><img decoding=\"async\" title=\"Image of command and associated output\" alt=\"Image of command and associated output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7120.hsg-10-22-11-2.png\" \/><\/a><\/p>\n<p>A WMI schema query queries the <b>meta_class<\/b> WMI class. It uses the <b>isa<\/b><i> <\/i>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:<\/p>\n<p style=\"padding-left: 30px\">$query = &#8220;select * from meta_class where __this isa &#8220;&#8221;$($class.name)&#8221;&#8221; &#8220;<\/p>\n<p>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:<\/p>\n<p style=\"padding-left: 30px\">$a = gwmi -Query $query -Namespace $namespace |<\/p>\n<p style=\"padding-left: 30px\">&nbsp; select -Property __class, qualifiers<\/p>\n<p>If the qualifiers contain the qualifier I am looking for, I return the WMI class name:<\/p>\n<p style=\"padding-left: 30px\">if($a.qualifiers | % { $_ | ? { $_.name -match &#8220;$qualifier&#8221; }})<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; { $a.__class }<\/p>\n<p>&nbsp;<\/p>\n<p>The core portion of the script, with the aliases removed is shown here:<\/p>\n<p style=\"padding-left: 30px\">Param([string]$qualifier = &#8220;dynamic&#8221;,<\/p>\n<p style=\"padding-left: 30px\">&nbsp; [string]$namespace = &#8220;root\\cimv2&#8221;)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;$classes = Get-WmiObject -list -namespace $namespace<\/p>\n<p style=\"padding-left: 30px\">&nbsp;foreach($class in $classes)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;{<\/p>\n<p style=\"padding-left: 30px\">&nbsp; $query = &#8220;Select * from meta_class where __this isa &#8220;&#8221;$($class.name)&#8221;&#8221; &#8220;<\/p>\n<p style=\"padding-left: 30px\">&nbsp; $a = Get-WmiObject -Query $query -Namespace $namespace |<\/p>\n<p style=\"padding-left: 30px\">&nbsp; Select-Object -Property __class, qualifiers<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; if($a.qualifiers | ForEach-Object { $_ | Where-Object { $_.name -match &#8220;$qualifier&#8221; }})<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; { $a.__class }<\/p>\n<p style=\"padding-left: 30px\">&nbsp; } #end foreach $class<\/p>\n<p>&nbsp;<\/p>\n<p>Well, that is about it for today. I hope you enjoy the function, and have an awesome weekend.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Use a Windows PowerShell function to find WMI classes with specific qualifiers. &nbsp; Microsoft Scripting Guy Ed Wilson here. In Thursday&rsquo;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 [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[69,3,4,45,6],"class_list":["post-12321","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-functions","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell","tag-wmi"],"acf":[],"blog_post_summary":"<p>Summary: Use a Windows PowerShell function to find WMI classes with specific qualifiers. &nbsp; Microsoft Scripting Guy Ed Wilson here. In Thursday&rsquo;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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12321","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=12321"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12321\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=12321"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=12321"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=12321"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}