Ezine 174 – Scripting WMI with PowerShell WMI

Ezine 174 – Scripting WMI with PowerShell WMI

This week we are going to see how easily Microsoft PowerShell can script Windows Management Instrumentation (WMI) objects.  For at least twenty years WMI has been the unsung hero for reporting hidden information about the operating system.  Many companies have made their reputations by producing third-party utilities, which use WMI to display such information that is not exposed by the Control Panel or the Windows Explorer.

Topics for Microsoft PowerShell and WMI

 ♣

This Week’s Secret

This week’s ezine marks the watershed between VBScript and PowerShell.  If you already have a passing familiarity with WMI from VBScript, then I am going to present you with a crucial decision.  Let us imagine that you are the coach of a scripting team, do you substitute VBScript and bring on the new ‘Wonder Kid’ PowerShell, or do you overlook PowerShell and continue with that aging ‘Old Pro’ called VBScript?  The rest of this ezine assumes that you want to make that substitution and check out what PowerShell can do with WMI.

Over the years enthusiasts and system administrators have developed numerous WMI scripts using VBScript.  Indeed, many of these scripts are as good as commercial offerings, it’s just they lack a flashy GUI and attractive packaging.  The good news is that we can use Microsoft PowerShell to make scripts with the same, or greater capabilities, but with half the lines of code.  I would encourage those with an aptitude for writing code to seek their fortune by creating commercial utilities.

This Week’s Mission

This Week’s Mission is to research the capabilities of PowerShell’s get-WmiObject.  Firstly, we will list all the WMI classes and then we will concentrate on those classes that deal with network objects.

In this ezine I also want to open your mind to the possibility that WMI scripts will solve a computer problem.  Naturally, I also want to highlight the important PowerShell features in WMI code, in the hope that you will 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 554.

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

Example 1: PowerShell Tutorial to List WMI Classes

Let us put the lessons of previous ezines into practice.  Although not all PowerShell cmdlets use the -list parameter, get-WmiObject does support this handy enumerator.  However, there is one problem with the basic command: get-WmiObject -list, namely the overwhelming number of classes.  For this reason I have introduced extra code which filters classes relevant to a particular project; this week’s project focuses on Network classes.

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.

Example 1a: To List All WMI Classes

One of the most useful subsets of WMI classes is the one beginning with Win32.  Example 1a will give you a count of the Win32 classes.

# PowerShell example to list all WMI classes
# Author: Guy Thomas
# Version 1.3 October 2008 tested on PowerShell v 1.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 ‘There are’ $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 a beginner. For simplicity, you could strip the script down to this one command:

get-WmiObject -list | Where-Object {$_.name -match "Win32"}

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

Example 1b: To List Only PowerShell Network Classes

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 PowerShell network classes.

# PowerShell network example to list Win32_network classes
# Author: Guy Thomas
# Version 1.3 October 2008 tested on PowerShell v 1.0

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

Learning Points

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 2: You could use the alias PowerShell gwmi instead of get-WmiObject

Note 3: See more on using WMI Monitor.

Example 2: How to Research WMI Properties with get-Member

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.

clear-Host
get-WmiObject -class Win32_NetworkAdapterConfiguration | get-Member

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.

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

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.

Example 3: Putting Win32_NetworkAdapterConfiguration to Work

I cannot emphasise enough that this is but one example of employing Win32; there are literally 1,000s of uses of WMI that I could have chosen.  It would make my day if, once you have mastered the basics, that you research more jobs for get-WmiObject.

This example employs Win32_NetworkAdapterConfiguration to display information about your computer’s IP address and MAC address.

clear-Host
get-WmiObject -class Win32_NetworkAdapterConfiguration | `
where-Object {$_.IPEnabled -eq ‘True’} | format-Table IPAddress, MACAddress

Learning Points

Note 1:  The tiny backtick ` tells PowerShell that the command word-wraps to the next line.

Note 2:  The ‘where-Object’ clause filters the network adapters, as a result you only see those which are enabled.

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

PowerShell’s Aliases

PowerShell supports numerous aliases, for example, you can shorten format-Table to its alias ft.  Because I want to help people get started I tend to use the full command, I therefore leave it up to each individual how much use they make of PowerShell’s aliases.  The trade-off is they are wonderful for shortening scripts, but can cause confusion if you have to explain your code to a greenhorn.

In the above examples you could try substituting PowerShell’s gwmi for get-WmiObject, and also plain ‘where’ for ‘where-Object’.

Guy Recommends: The Free IP Address Tracker (IPAT) IP Tracker

Calculating IP Address ranges is a black art, which many network managers solve by creating custom Excel spreadsheets.  IPAT cracks this problem of allocating IP addresses in networks in two ways:

For Mr Organized there is a nifty subnet calculator, you enter the network address and the subnet mask, then IPAT works out the usable addresses and their ranges. 

For Mr Lazy IPAT discovers and then displays the IP addresses of existing computers. Download the Free IP Address Tracker

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 WMI classes.  My secondary objective is to give you a practical example of a WMI script which will provide extra information about your computer’s network adapter.  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.