Windows Management Instrumentation (WMI) provides a way of accessing
details of your operating system that are normally hidden from the
Control Panel, Device manager or Windows Explorer. Every time I
review WMI it amazes me the number and variety of these probes, or WMI
Classes that are available to PowerShell's get-WmiObect.
Although not
all PowerShell cmdlets use the -list parameter,
get-WmiObject does support this handy enumerator. However, there is
a problem with the base command: get-WmiObject -list, namely the
overwhelming number of classes returned. For this reason I have introduced
extra code which filters classes relevant to a particular project; for
example, network classes.
One of the most useful subsets of WMI classes is the one beginning with Win32.
This example will give you a count of the Win32 classes.
# PowerShell example to list all Win32 WMI classes # Author: Guy Thomas # Version 1.4
February 2010 tested on PowerShell v 1.0 and 2.0
clear-Host $i=0 $Type = "Win32" $WMI = get-WmiObject
-list | Where-Object {$_.name -match $Type} Foreach ($Class in $WMI) {$Class.name;
$i++} Write-Host 'These are the ' $i' types of '$Type
Learning Points
Note 1: This script employs 3 variables, $i to count the
instances, $Type to hold the string you wish to filter, and $WMI to initiate the
loop which counts the object classes.
Note 2: 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:
Challenge: Amend my example script to display extra information
Challenge: Observe and trace how PowerShell's -match parameter compares the value held
by $Type. Incidentally, you could experiment with "CIM" instead of
"Win32".
Guy
Recommends: WMI Monitor and Its Free!
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft operating systems. Fortunately, Solarwinds
have created the
WMI Monitor so that you can examine these gems of
performance information for free. Take the guess work out of which
WMI counters to use for applications like Microsoft Active Directory,
SQL or Exchange Server.
The additional feature of this script is that refines the search
from the broad 'Win32', to the narrower 'Win32_Network'. The
result is a list of network WMI classes.
# PowerShell example to list every WMI class matching Win32_network # Author: Guy Thomas # Version 1.5
February 2010 tested on PowerShell v 1.0 and 2.0
Note 1: In practical terms, most of the 7
network classes are disappointing. However, this is a feature of
WMI, it sounds exciting to find so many classes, but when it comes down
to practicalities, only one or two are truly useful for any given task.
Note 3: You could use the alias PowerShell gwmi instead
of get-WmiObject
Nearly everyone who scripts with PowerShell calls for help with get-Member. Some experts freely
admit employing this technique, while others pretend that they can
memorize all the properties. (One or two sad cases really can, and
consequently, will quote all the Properties of any class you care to name).
I cannot emphasise too strongly how important get-Member is in mastering
PowerShell. Not only can you apply get-Member to every other PowerShell
cmdlet, but also third-party add-ons such as QAD support this help system.
Note 1: The secret to success with get-Member is to remember
the (|) pipeline symbol before you append this get-Member cmdlet.
Note 2: If you are overwhelmed with
information you can add the -MemberType parameter with a value of
'property' hence: get-Member -MemberType property [a-z]*.
Note 3: Before we leave get-Member a
reminder that even this command accepts get-Help, thus you could try:
get-Help get-Member -full. My point is that help will reveal other
options, for example -MemberType Method.
Summary of Types of WMI Class
The key information that PowerShell's get-WmiObject needs is a WMI class.
The examples on this will help you to research the best class for your task.
Those WMI Classes beginning with Win32 are a particularly rich source,
however to refine your search try adding a PowerShell -match statement.
Please write in if you see errors of any kind. Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit.
Guy
Recommends: Orion's NPM - Network Performance Monitor
Orion's performance monitor is designed for detecting network outages.
A network-centric
view make it easy to see what's working, and what needs your attention.
This utility guides you through troubleshooting by indicating whether the
root cause is faulty equipment or resource overload.