Searching WMI

Ladies and Gents

A lot people have been asking me: “How do I know if there is a WMI provider for xxxx?”. Quite often a quick search in live.com will get you the results, but there is a way of searching WMI from within PowerShell. I have created a really simple script that allows me to search the CIMV2 namespace really easily. Here’s the script:

$text = $Args[0]

write-host

"Searching Namespace: root\cimv2 ...... "

$Results = gwmi -namespace "root\cimv2" -list | where-object {$_.name -like ("*" + $text + "*")}

if ($Results -eq $Null)

{

     write-host

     write-host "No Matches for:" $Text

     write-host

}

else

{

     write-host

     write-host "Class"

     write-host "-----"

     write-host

     foreach ($Class in $Results)

     {

           write-host $Class.Name

     }

     write-host

}

 

 

The code to do the searching is 1 line and simply uses where-object to filter the root\cimv2 namespace. The rest is just for iterating round the collection of $Results and formatting it nicely. I know I could write this in a 1 liner each time I wanted to search WMI, but I`m lazy. Using this I can concentrate on WMI which is tough enough and not have to think about how to search it J

I then create a function that hooks to this script. I put the following line in my profile in order to do this:

 

new-item -path function:search-cimv2 -value {& 'C:\scripts\search-cimv2.ps1' $Args[0]}

 

Simply type in search-cimv2 disk and it will return all the WMI classes in the CIMV2 namespaces that contain the text ”Disk”. Note the use of * in the script that means it will fine the text anywhere in the class name.

 

Hope this helps

 

BenP