Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Searching for a CIM/WMI Class with PowerShell

Posted on September 18, 2018September 18, 2018

I got a question on Twitter about an older function I has posted to get antivirus information via WMI. The function continues to work fine with Windows 10, although there's always room for improvement. However, the question was that the function did not seem to work when querying a server running Windows Server 2016 or later. And that does appear to be the case. From what I can tell the WMI namespace my function is querying does not exist on Windows Server 2016 and later. I figured I needed to search to see if there were antivirus products anywhere else.  So I wrote a function this morning to search all WMI namespaces for a class name.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

My function, Find-CimClass, uses the CIM cmdlets to recursively search all namespaces on a computer (the default is the localhost) for a given class name. You can use wild cards for the class name. I also included an option for an Exclude pattern which can be a regular expression pattern. For example, I may search for a class name but want to exclude any of the Win32_Perf* classes. The function can be found as a gist on my GitHub repository.

https://gist.github.com/jdhitsolutions/66722c63fbd244904e0a3b09cd9909bd

Because I'm making repeated queries, I create a temporary CIMSession. I don't really need it when querying the local machine and could have added code to only create the CIMSession if the computername is remote. But for the sake of simplicity I create a temporary CIMSession regardless. The other scripting element you'll see in the function is the use of Write-Progress. The function will take a little bit of time to complete and I wanted to provide feedback.

image

As you look through the code I hope you'll realize that using Write-Progress is not that difficult. I think more scripters need to take advantage of this command.

With this tool in hand, I searched for anything antivirus related on a Windows Server 2016 box but with no results. I'll be honest that I have not researched this issue in great deal, but is my searches thus far into WMI it appears Microsoft has removed any related namespaces and classes or changed them to something that I haven't thought of or discovered.

Regardless, I now have another tool in my toolbox to easily discover things in WMI. I hope you'll let me know what you think.

Update:

Shortly after publishing the original article and function, I realized the logic I was using to enumerate namespaces was incomplete. I was only getting the first 2 levels of namespaces. I ended up adding an internal function to recursively list all namespaces. Where I was search 57 before on my Windows 10 box, now I am searching 150. Unfortunately, this didn't affect the search results for an antivirus class on Windows Server 2016.

Update #2:

After searching for anything I could think of I stumbled across the ProtectionTechnologyStatus class in the Root\Microsoft\SecurityClient namespace on Windows Server 2016. This appears to have all of the relevant information.

 

 


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

2 thoughts on “Searching for a CIM/WMI Class with PowerShell”

  1. Tsvika says:
    November 1, 2018 at 11:22 am

    Does this article have the code for Find-CimClass?

    1. Jeffery Hicks says:
      November 1, 2018 at 11:31 am

      The code is embedded as a gist on GitHub.

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d