Ezine 146 – Investigating PowerShell and WMI’s Many Classes

Investigating PowerShell and WMI’s Many Classes

Last week we investigated one specific WMI object, namely the win32_logicaldisk.  This week my message is there are an unbelievable number of other WMI objects.   Moreover, one day you will need one of these WMI object classes to solve a particular problem.  I want to prepare you for that ‘one day’ by showing you how to search for interesting WMI objects.

Topics WMI’s Many Classes

 ♣

This Week’s Secret

When I visit the supermarket, as I am waiting in the checkout queue, my mind sometimes wanders to thoughts like, ‘Who buys these goat products?’ or, ‘Who on earth drinks that strange plum wine?  Does anybody really buy those gingerbread men?’  WMI classes remind me of such superfluous goods displayed in the supermarket.  In both cases there are zillions of items, we each have our favorite fare, but we don’t have any use for most of the other products.

With WMI classes, most will be of no use to you, but somewhere, somebody is using them enthusiastically.  If I could stretch my analogy, one day you may host a party, and for that occasion you may need some strange wine for aunty, and gingerbread men for the kids.  So it is with WMI classes, one day you will have a task that you can only achieve by copying someone else’s script, and that script DOES use a strange WMI class, one that you previously thought was useless.

This Week’s Mission

In this ezine my mission is to open your mind to the possibility that WMI scripts will solve a computer problem.  I also I want to highlight the important PowerShell features in WMI code, so that you can modify my script to suit your circumstances.  We will start by listing all the available WMI classes which begin with Win32.  To digress, my friend ‘Mad’ Mick asked me, ‘How many Win32 classes do you think there are?’ I guessed 150 classes beginning with Win32.  To my amazement, when we ran the Example 1 script, the answer was 602.

Once we have enumerated all the Win32 classes, I will fixate on Win32_SystemSlot and tackle a practical mission to investigate your computer’s slots (PCI, ISA and AGP).  I will also remind you how to examine the object’s properties, from the results, we will choose suitable properties to incorporate in the script.

Example 1: Discover WMI classes beginning with Win32

One of the most useful subsets of WMI classes is the one beginning with Win32.  This script will give you a count of the Win32 classes.  With a simple modification you could make the script count and list the CIM* classes (objects).

Instructions:

Pre-requisite: Visit Microsoft’s site and download the correct version of PowerShell for your operating system.

  • Launch PowerShell
  • Copy the six lines of code below (into memory)
  • Right-click on the PowerShell symbol
  • Edit –> Paste
  • Press enter to execute the code.

clear-Host
$i=0
$Type = "Win32"
$WMI = get-wmiobject -list | Where-Object {$_.name -match $Type}
Foreach ($CIM in $WMI) {$i++}
Write-Host ‘There are ‘$i’ types of ‘$Type

Learning Points

Note 1:  One side effect of introducing extra PowerShell techniques, is that I have made the script unnecessarily long and complex.
For simplicity, you could strip the script down to this one command:
get-wmiobject -list | Where-Object {$_.name -match "Win32"}

Note 2: This script employs 3 variables, $i to count the instances, $Type to hold the string you wish to filter, and $WMI to enable the loop which counts the object classes.

Note 3:  Observe how PowerShell’s -match parameter compares the value held by $Type.  Incidentally, you could experiment with "CIM" instead of "Win32".

Note 4: This example merely counts, but does not actually list the 600+ Win32 classes.  However to see a listing, please accept my challenge below.

Guy Recommends: WMI Monitor and It’s Free!Solarwinds Free WMI Monitor

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 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

Challenge – Get a list of Win32* objects

In order to get a list, you could simple add an extra line to the above script; then on that new line type:
$WMI

Note 1:  You could learn even more about the object by appending | get-member:
clear-Host 
$WMIPort = get-wmiobject -class "Win32_SystemSlot"
$WMIPort | get-Member -memberType properties.

Example 2: Examine the system slots. (PCI, ISA and AGP)

What my Example 2 script does is list the slots on your computer’s system board.  The crucial WMI class is the aptly named, Win32_SystemSlot.   We can also investigate its properties, for example, SlotDesignation, Status, and CurrentUsage.  My overall point is that WMI can reveal data which is not available via the Hardware Device tab of the System Property sheet.

clear-Host
$WMIPort = get-wmiobject -class "Win32_SystemSlot" -computername ‘.’
$WMIPort | format-Table Tag, SlotDesignation, CurrentUsage, Status -auto

Learning Points

Note 1:  At the heart of this script is: get-WmiObject.  We then filter for the object that we are interested in namely: Win32_SystemSlot.

Note 2:  The -computername parameter is optional.  I chose the ‘dot’ value so that it will work on any computer.

Note 3:  Once again, you could shorten my script to this simple command:
gwmi -class "Win32_SystemSlot" | ft Tag, number, SlotDesignation -auto

Challenges

Challenge 1:  Why not append a command to interrogate another computer:
-computername YourComputer.

Challenge 2: You could re-visit get-member and experiment with other properties, for example, Shared, MaxDataWidth or Number.

Guy Recommends: Tools4ever’s UMRAUMRA The User Management Resource Administrator

Tired of writing scripts? The User Management Resource Administrator solution by Tools4ever offers an alternative to time-consuming manual processes.

It features 100% auto provisioning, Helpdesk Delegation, Connectors to more than 130 systems/applications, Workflow Management, Self Service and many other benefits. Click on the link for more information onUMRA.

Summary: PowerShell and WMI’s Many Classes

Get-WmiObject opens up a Pandora’s Box of scripting classes or objects.  The main purpose of this page is to get a listing of all possible classes.  My secondary objective is to give you a practical example of a WMI script which will provide extra information about your computer’s PCI or ISA slots.  Finally, remember when ever you find a new PowerShell object try | get-member -memberType properties.

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

 


See More Microsoft PowerShell WMI Examples:

Home   • PowerShell Get-WmiObject   • Windows PowerShell   • PowerShell 3.0 Network

Win32_pingstatus   • WMI Win32_NetworkAdapter   • Win32_NetworkAdapterConfig

Disable NIC   • PowerShell -Filter  • PowerShell -Query   • PowerShell Select   • Free WMI Monitor

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