Guy’s Scripting Ezine 110 – PowerShell WMI

Guy’s Scripting Ezine 110 – PowerShell WMI


This Week’s Secret

This week Microsoft announced that Monad had metamorphosized into PowerShell.  (My friend ‘Mad’ Mick unfairly calls it PowersHell.)  One of the hazards of being a Beta pioneer is having to keep updating to the latest version of the program.  What I did was download PowerShell, uninstall Monad and install the new PowerShell.  The crucial difference is that PowerShell files require a change of extension from .msh to .ps1.  This new extension only affects scripts, called cmdlets, which we will investigate next week.

This Week’s Mission

With any new system you get hype.  In the case of PowerShell much of the talk is about what it may (or may not) do in Longhorn.  What I want to do is give you a killer reason to use PowerShell, with Windows 7, or Windows Server.  That killer reason is to create WMI scripts.  I urge you to switch to PowerShell rather than use VBScript for accessing WMI.  If you have never explored WMI, then it’s like an Aladdin’s cave, so start now with PowerShell.  Here is an example, which discovers your computer’s name, model, manufactures and memory, type:

get-WmiObject win32_computersystem

If there is another machine on your subnet try these two commands:
help WmiObject  (I mention help for you to see how I knew it was -computername)
get-WmiObject -computername  othermachine win32_computersystem

Calling for Windows Management Instrumentation (WMI) is rather like switching on a microscope to observe what the operating system is doing.  WMI permits you to discover what the operating system knows about disks, memory processor or any other Win32 component.  My prediction is not the end of the road for WMI, far from it, what I foresee is using PowerShell commands rather than VBScript to access the WMI classes.

Note: See more on using WMI Monitor

Guy’s Twin Goals for PowerShell and WMI

1) To explore WMI’s Win32_* classes type; get-WmiObject.  If at least one of these examples does not give you an idea for a production PowerShell scripts then I will be amazed.

2) I want to introduce you to a few useful PowerShell commands that you learn once then apply to a wide range of verb-noun pairs. For example, format-table, -autosize and -list.

Assumptions and Preparation

You download PowerShell (or Monad) and .NET Framework 2.0.  Next, you install the .msi file, finally you run powershell (or for Monad, MSH).  Now you are at a black screen with a PS Prompt >

Note:  When you type on the PowerShell command line, the only tricky symbol is this pipe |.  If in doubt activate your keyboard’s number lock, hold down the alt key, type 124 and then let go of alt.  If necessary, investigate which key on your keyboard corresponds to this pipe symbol (|). 

Here are commands to try at the PS Prompt >

i a) get-WmiObject Win32_NetworkAdapterConfiguration

i b) get-WmiObject Win32_NetworkAdapterConfiguration | format-table

i c) get-WmiObject Win32_NetworkAdapterConfiguration | format-table Ipaddress, Description, DHCPEnabled -autosize

ii a) In many ways we should have started by researching a list of properties, try:
get-WmiObject Win32_NetworkAdapterConfiguration | get-member

ii b) Now amend this command choosing different properties:
get-WmiObject Win32_NetworkAdapterConfiguration | format-table IPAddress, Description, DHCPEnabled -autosize   For example: DefaultIPGateway.

iii a) get-WmiObject Win32_product

iii b) get-WmiObject Win32_product |sort-object vendor |format-table name, vendor -groupby vendor

iv) get-WmiObject Win32_LogicalDisk | format-table

v) get-WmiObject Win32_environment

vi a) Research more objects with: get-WmiObject -list

vi b) Refine your search with: get-WmiObject -list | where {$ -like "Win32_n*"}

vi c) Try Win32 objects beginning with other letters, for example, substitute ‘p’ "Win32_p*.

vii) As you make your selections try appending at least some of these,
| format-table name, others
| format-table name, others, -autosize
| sort-object
| where {$ -like "Win32_n*"}  Note the braces type brackets.
Also remember; WmiObject Win32_xyz  | get-member

See more PowerShell Scripts for Windows 7

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

Mad Mick and Mapnetworkdrive.

While ‘Mad’ Mick was rude about PowerShell, he is a genius when it comes to scripting.  His first question to me was ‘Guy can PowersHell map network drives’.  I tried for half an hour then said, ‘It does not seem possible Mick’.  Then he bet me a curry that in ten minutes he could get it to map a local drive letter to a UNC path.  I lost the bet, but learned a valuable lesson.

Before we try the code that Mick produced, please carry out a ‘before’ experiment, simply type:
get-psdrive  (Note the singular noun, also note ‘PSdrive’, it was plain ‘drive’ in monad).  One more ‘before’ experiment try
get-WmiObject win32_networkconnection

The main event.  Here is the code Mick created, please alter the UNC path otherwise it will not work.  Also, please place the commands on two lines with a carriage return, no worries if you just copy and paste!

$net = $(New-Object -Com WScript.Network)
$net.MapNetworkDrive("s:", "\\grand\ezine")

If you get an error, read the message!  Check the UNC path.  If it works confirm with
get-WmiObject win32_networkconnection.

See more on PowerShell 3.0 Get-NetIPConfiguration

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 of PowerShell and WMI

I have two goals for PowerShell, my main goal is to show you what PowerShell can do for you here and now with WMI.  My secondary goal is to introduce useful general PowerShell commands, for example, append instructions with a | (pipe) then format-table,-list or -groupby.

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.