PowerShell Function Get-WMIClass

How to Filter the WMI ClassesPowerShell Function Get-WmiClass

My aim is to provide an easy way to search for a class amongst the 800+ WMI (Windows management instrumentation) objects.

I will provide instructions for creating a Windows PowerShell function; in particular I want to highlight creating parameters.

Guy's Get-WMIClass Function (Cmdlet)

 ♣

Planning My Get-WMIClass Function

The purpose of this function is to make it easier to filter the 800+ WMI classes based on a text string in their name.  Before I wrote this function each time I needed a particular WMI Class, I had to struggle with the Where clause and -Match comparator.

1) Comments Section – Help and Description
I leave creating this section until I have completed the rest of my function.

Note 1: .SynopSIS, and other keyword comments, are not case sensitive.

Note 2: .SYNOPISIS
Each keyword must be on its own line, with the description is underneath.

You can get a complete guide on the keywords with:
Get-Help about_Comment_Based_Help

2) PowerShell Function Parameter Examples
This Param example features both [String] and [Switch] parameters, trace how each type is treated differently in the Process section of the function below.

[Switch] Parameter: ($Network) {
$a = Get-WmiObject -list | Where-Object {$_.name -Match "Network" `
If you employ -Network the function delivers whatever is hard-coded in the Where clause.

[String] Parameter $Class:
$a = Get-WmiObject -list | Where-Object {$_.name -Match $Class `
In the case of this string parameter, the $Class variable delivers whatever you type AFTER -Class ….

3) Process – PowerShell Keyword
This contains the engine of your script.  It could contain one elegant PowerShell command, or else a series of If … ElseIf statements, which refine the base Get-WmiObject command.

4) Begin and End
These keywords (Begin, End) are optional sections, and have not been included in this function example.

Instructions for Creating a PowerShell Function

Function Global:Get-WMIClass {
<#
.SYNOPSIS
A function to list WMI classes.
.Description
Contains switches to filter for network WMI classes.
.Example
Get-WMIClass -Network
.Example
Get-WMIClass -Class Computer
#>
[cmdletbinding()]
Param (

[Parameter(Mandatory=$False,
Position=0,
ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$True)]
[Alias('cl')]
[String]$Class ="", #End of $Class parameter definition
[Switch]$Network
      )
Begin {
Clear-Host
"Retrieving WMI Classes …"
         } # End of Begin section
Process {
If ($Network) {
$a = Get-WmiObject -List | Where-Object {$_.name -Match "Network" `
-And $_.name -Notmatch "Win32_Perf"}
Clear-Host
$a
" `n There are " + $a.count + " " + $Class + " WMI classes "
" (Not counting Win32_Perfxyz counters)"
             } # End If
Else {
$a = Get-WmiObject -list | Where-Object {$_.name -Match $Class `
-And $_.name -Notmatch "Perf" }
Clear-Host
$a
" `n There are " + $a.count + " " + $Class + " WMI classes "
       } # End of Else
    } # End of Process section
End {
"`n " + (Get-Date).DateTime
      }
} # End of Function

3 Commands to try with my example function

  • Get-Help Get-WMIClass -Full
  • Get-WMIClass -Network
  • Get-WMIClass -Class msft  (Can also use wildcard*)

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor

PowerShell Function Learning Points

Process
If you take away just one PowerShell function concept from this script, it's that Process is not only a keyword, but also it is the heart of the script.  In this instance, my 'Process' section is very much based on PowerShell's built-in Get-Service cmdlet.

It's not uncommon to create functions which just encapsulate what you can achieve with a normal cmdlet / function.  This is an example where I have bolted-on a Where clause to the normal Get-Service cmdlet.

Parameter Problems and Improvements
I began using only the default [String] type of parameter.  Then I realized it was better to define -Network as a [Switch] parameter.  Naturally, I then modified the rest of the script to suit this construction. 

It's also possible to declare parameters using a simpler style of syntax; note the parameters are listed before the first brace {.

Function Global:Get-WMIClass ([String]$Class,[Switch]$Network) {

Instead of using the explicit Param syntax:
——————————————–

Function Global:Get-WMIClass {
Param (
[String]$Class ="",
[Switch]$Network
)

Process Keyword and Section
It's instructive to study how in the Process section the [String] param uses {$_.name -Match "Network" – try "Netw"; whereas the [Switch] uses nothing but $Class,  {$_.name -Match $Class.

One problem with declaring the $Class param as [String] is that the user has to guess what to type after -Class.  With [Switch] it automatically executes the script without any further user intervention.

Begin and End Keywords
While clearly not essential, I enjoyed adding a Begin and End Section to clear the screen and add a time.

Guy Recommends: Free WMI Monitor for PowerShellSolarwinds Free WMI Monitor for PowerShell

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft’s operating systems.  Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your PowerShell scripts.

Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory, or Exchange Server. Give this WMI monitor a try – it’s free.

Download your free copy of WMI Monitor

Problems in Creating PowerShell Functions

Experts make it look easy, but for a beginner it is a major challenge to create a PowerShell Function.  Because at first it's hard to understand the syntactic rules, it's easier to give up and get the task done without using functions.

There is good news, creating a PowerShell function is most satisfying, and when you are learning you can just ignore factors such as Begin, End and .Synopsis.

My technique was to learn by doing, to build on success, and gradually add to my code terms such as Global:, [String] or [Switch] for parameters.  The error messages directed me to significance of the different bracket styles as well as the correct use of the comma.

See more on PowerShell function parameters ยป

Summary of How to Filter the WMI Classes

This page has instruction on creating a PowerShell function which will search for an object amongst the 800+ WMI classes.  I have explained how the various sections of a PowerShell function. 

If you like this page then please share it with your friends

 


See More PowerShell Function Parameters

Scripting PowerShell Function   • PowerShell Function Clear-Recyclebin   • PowerShell Get-File

Create PowerShell Function Get-ServiceStatus  • PowerShell Function Get-Driver  • PowerShell Outlook

Show-BalloonTip   • PowerShell Function Get-IPConfig   • Free Permissions Analyzer

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.