Ezine 200 - PowerShell and WMI
Ezine 200 - PowerShell and WMI
The goal of this week's ezine is to introduce PowerShell newcomers to the
benefits of WMI (Windows Management Instrumentation), and to persuade those
familiar with VBScript that WMI is the killer reason to make the switch to
PowerShell.
To learn more about WMI, and have some fun, I recommend you try Solarwinds free WMI
Monitor.
Topics for PowerShell's Get-WmiObject (Gwmi)
This WMI Monitor enables to you to measure the real-time
performance of any modern Microsoft operating system. Your FREE WMI
Monitor will deliver a desktop dashboard that monitors any Windows server and offers built-in
templates plus community customizable templates. Take the guess work out of which WMI counters to
use for applications like Microsoft Active Directory, Exchange mailboxes or SharePoint.
WMI Monitor Highlights:
- Monitor real-time performance metrics on any Windows computer or
application.
- Leverage a large selection of pre-built and community generated
application templates.
- Modify or design your own application templates with the
built-in WMI browser.
- Use the WMI Monitor to familiarise yourself with classes that
can be then used for PowerShell scripting.
-
Download Your Free WMI Monitor
See more on WMI Monitor
Firstly, a reminder that PowerShell cmdlets are not case sensitive, thus all these
are interchangeable, get-wmiobject, get-WmiObject, Get-WMIObject and
similar capitalizations. Just remember the verb is 'get' and the noun
is 'wmiobject'.
Let us ask for help with this important WMI PowerShell cmdlet.
-Class This is the most important of
get-WmiObject's parameter (some people call them switches). When you are
doing real world stuff with get-WmiObject you need to specify which of the
hundreds of objects you need. The only reason you don't see more of
this parameter is that it can be 'assumed' if you follow get-WmiObject
immediately with the name of an actual class.
Last week used at PowerShell's service cmdlets, here is an
alternative method using WMI.
get-WmiObject win32_service # Is the equivalent of: get-WmiObject
-Class win32_service
-Filter is like using a where clause, but it's
easier and faster at returning the results.
One source of confusion is that -Filter uses syntax from WQL.
This means that -filter uses the traditional equals sign, whereas
pure PowerShell commands use -eq. You can see what I mean in
the following example, filter uses "Name = 'SNMP' ", yet
where-Object uses '-eq SNMP'.
get-WmiObject win32_service -filter "name = 'SNMP' "
# Here is the
'Where' clause alternative: get-WmiObject win32_service | where-Object
{$_.name -eq 'SNMP'}
Note 1: I often abbreviate where-Object to plain
where, actually this technique makes use of the 'where' alias.
Note 2: Going back to examples of -Class, you could write:
get-wmiobject -filter "name='SNMP'" -class win32_service
Yet I regard
the previous line as inefficient and confusing. However it does
neatly explain an 'assumed' parameter.
-List on its own creates information overload. By
default it returns all the objects in the root\CIMV2 namespace. Narrow
your research with a where clause:
get-WmiObject -list | Where{$_.name -like 'win32*'} | sort name
#
You probably need a finer filter such as: get-WmiObject -list |
Where{$_.name -match 'network'}
Note 1: Experiment with different comparators,
for example:
-like takes a wildcard* However, -match collects your word anywhere in the
name of the class, and in this case * would be translated as a
literal, thus avoid wildcards with
-match.
-ComputerName causes me a teaching dilemma. Remoting is
the best new feature of PowerShell v 2.0, however, its parameter
-ComputerName can be one of the most troublesome aspects of PowerShell to
get working because you have to understand, and then master WinRm. Thus in
my desire to build-up gradually, I say first master then get-WmiObject then
make 'Remoting' and the -ComputerName switch a separate learning project.
To research a promising WMI class, investigate further
with get-Member thus:
get-WmiObject -Class Win32_ComputerSystem | get-Member
# Better
still filter for just properties get-WmiObject
Win32_NetworkAdapterConfiguration |` get-Member -MemberType Property
Note 1: PowerShell has no word-wrap, thus we use
the
backtick ` to tell the command to continue on the next line.
Trap: With the backtick there should
be no space after the `.
PowerShell Script to Extract NIC Information
get-WmiObject -class Win32_NetworkAdapterConfiguration ` -filter
"IPEnabled = True" | ` format-Table IPAddress, DefaultIPGateway,
MACAddress -auto
As you get to know me, you will get used to me saying, 'It would
make
my day if you would amend my script to your network'. Well the
above example is crying out for you to add extra properties.
♣
Guy's take on WMI is that the
operating system knows everything, and thanks to WMI we can get a glimpse of
objects and properties that are normally hidden. What's mind boggling
is how many Classes of these objects that WMI uncovers. Incidentally,
once VBScripters see how easy WMI is in PowerShell, it becomes an incentive
to make the switch.
Let us put into practice what we learned in a previous ezine, namely that a
PowerShell alias will reduce typing. Superficially GWMI looks like an acronym such as you would find for a bus
company, and gwmi sounds like a tropical disease. However, in the
context of PowerShell either GWMI or gwmi is an alias for get-WmiObject.
Have you noticed how the top people in any profession, or sport,
always do the basics so well? Probably because they get into good
habits by practicing the simple things until they become second nature.
We can apply these sound principles to PowerShell by always looking to
apply at least one of these simple techniques, get-Command, get-Help or
get-Member. To make sure that you have not missed anything,
review: get-Help get-WmiObject -full.
If you are looking for handy network utilities, try some of the free downloads at
Tools4Ever
If you like this page then please share it with your friends
See More Microsoft PowerShell WMI Examples:
• Home •
PowerShell Get-WmiObject •
PowerShell -Query •
PowerShell Select
• Win32_pingstatus •
WMI Win32_NetworkAdapter •
Win32_NetworkAdapterConfig
•
Disable NIC •
PowerShell -Filter •
Windows
PowerShell •
PowerShell 3.0 Network
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.
Download my ebook: Getting Started with PowerShell - only $9.25
You get 36 topics organized into these 3 sections: 1) Getting Started 2) Real-life tasks 3) Examples of Syntax.
In addition to the ebook, you get a PDF version of this Introduction to PowerShell ebook It runs to 120
pages of A4.
|