Ezine 181  PowerShell, WMI, Parameters and Classes

Ezine 181 PowerShell, WMI, Parameters and Classes

WMI (Windows Management Instrumentation) and PowerShell are a great partnership for scripting. This duo not only reinforces basic PowerShell techniques such as get-Help or get-Member; but also emphasises the object nature of modern scripting by accessing WMI’s 500+ classes.

This Week’s Secret WMI Classes

About a third of all my PowerShell scripts involve WMI.  Although each of this week’s examples only gives you a snippet of information, together the scripts tell you all you need to get started with this branch of Windows scripting.  Incidentally, PowerShell working with WMI reminds me why PowerShell needs .Net Framework for its other cmdlets to be effective.

Remember, the operating system knows everything.  Therefore what we what is a programmatic hook into that knowledge – enter WMI.  Thankfully all modern Windows operating system have WMI installed by default, thus we only need to concentrate on creating a script which fetches information on a property that we are interested in.  In future ezines, when we are confident with the parameters and syntax, we will use WMI to change a value on one of these classes.

WMI comprises of 500 classes, your first decision is which one to call via the get-WmiObject cmdlet.  This process is much easier in PowerShell than VBScript, because a cmdlet called get-WmiObject does the work of about 10 lines of VBScript code.  Incidentally, many people prefer the alias gwmi, but I prefer to avoid confusion and stick with the full verb-Noun paring.

A more technical description of a WMI class is, ‘A succinct description of the properties of a managed resource, and the actions that WMI can perform to manage that resource’.  In truth I had to read that last sentence twice before I understood it.  However, if you recall the word ‘Windows’ in WMI, then it will be no surprise that there is a detailed technical explanation of every aspect of WMI on Microsoft’s MSDN website.

This Week’s Missions

Our main real-life mission is to investigate the WMI class called Win32_ComputerSystem.   As we will see with get-Member, this class has a rich source of operating system properties from the domain name and number of processors to the logged on username.  I always find that Win32_ComputerSystem is a rewarding subject for scripting because it displays values that I can immediately relate to. 

On the theory side, I want to spend time examining and understanding the roles of PowerShell’s parameters, or switches as some people call them.

Research the Parameters

In my opinion each PowerShell cmdlet benefits from researching its parameters, I always learn something new.  This week mission is to show the significance of a parameter’s position, and also to explain the assumed nature of many parameters.

Because there are so many operating system ‘Objects’ or ‘Manageable Resources’ we need a WMI qualifier in the form of a ‘Class’.  In fact, get-WmiObject is pretty near useless on its own, we always need a ‘class’ in position one, for example, get-WmiObject Win32_NetworkConnection.

Strictly speaking, the PowerShell statement is:
get-WmiObject -Class Win32_NetworkConnection.

However, because PowerShell looks for ‘class’ as the first parameter, we can omit this word and just put the name of our ‘Managed Resource’ immediately after get-WmiObject.  Another way of looking at this statement is that if the word immediately following get-WmiObject is the valid name of one of the 500+ objects, then we don’t explicitly need to type the parameter -Class, PowerShell assumes it’s there.

See more about PowerShell Parameters

PowerShell’s ‘Remoting’ Problems, and WMI’s Remoting Solution

There are good reasons for pausing to take a good look at WMI’s -ComputerName parameter.  ‘Remoting’ is one of the flakiest aspects of PowerShell v 1.0, and why I am impatient for the release of the final version of PowerShell v 2.0.  The clever trick is to employ WMI and use its superior capabilities to ‘remote’ to other computer’s operating systems.

Naturally, the default is the local machine.  Indeed specifying any other machine may cause the script to fail, but this may not be WMI’s fault.  The problem is most likely caused by firewall settings or insufficient administrative rights on the target machine.

One work-around in PowerShell v 1.0 is to employ WMI’s superior ability to remote to another computer.  For example, instead of using PowerShell’s native get-Eventlog, try WMI’s Win32_NTLogEvent -ComputerName.  However, I would only recommend this technique if you need to access event logs on other machines on your local network.  Oh for PowerShell v 2.0 where ‘Remoting’ with the -ComputerName parameter works so much better.

Pre-Requisites

Before you test my short scripts you need to download and install PowerShell and .Net Framework.  Next you launch the executable ‘PowerShell’, now you are ready to type (or copy and paste) my short scripts.

Example 1: Research get-WmiObject’s Parameters

# PowerShell example to list demonstrate get-WmiObject
# Author: Guy Thomas
# Version 1.2 February 2009 tested on PowerShell v 1.0

get-Help get-WmiObject -full

get-WmiObject’s parameters: -class -property -namespace

Get-WmiObject is one of the most important and productive of PowerShell cmdlets.  This is why it’s worth examining for itself, and also as an example of how to research ANY cmdlet’s parameters (some call them switches).

Example 2: PowerShell Script to List ‘Classes’

You could begin by trying plain:
get-wmiobject -list

The rest of the script is a convoluted way of filtering the list of classes.  My idea is to control the filter via the variable $Type.  Indeed, it would make my day if you amended my script, for example by substituting "Computer" for "Win32_network".

# PowerShell example to list Win32 network classes
# Author: Guy Thomas
# Version 1.2 February 2009 tested on PowerShell v 1.0

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

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 3: Real-life Task – Check Your ComputerSystem

Many of the WMI classes begin with Win32, thus the object that holds general machine information is called Win32_ComputerSystem.  From the properties of this object we can display information about the system type, the domain (if any), even the logged on user.  However, for this example I have chosen: NumberOfLogicalProcessors and TotalPhysicalMemory.

# PowerShell example to examine Win32_ComputerSystem
# Author: Guy Thomas
# Version 1.2 February 2009 tested on PowerShell v 1.0

get-WmiObject Win32_ComputerSystem | `
format-Table name, NumberOfLogicalProcessors, TotalPhysicalMemory

Learning Points

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

Note 2:  How did I know the property was called precisely: NumberOfLogicalProcessors?  A lucky guess, no, it was thanks to get-Member.

Challenge: Employ get-Member to unearth more properties.

# PowerShell
# Author: Guy Thomas
# Version 1.2 February 2009 tested on PowerShell v 1.0

get-WmiObject Win32_ComputerSystem | get-Member

Summary of Pairing PowerShell and WMI

My two practical missions are to research WMI classes, and then for any given class, to add suitable properties to your script.  If you achieve these goals then along the way you will not only get a feel for the WMI syntax, but also understand about objects and properties.

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.

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.