Mr. Roboto

Your First Fishing Lesson

In anticipation of PowerShell 2.0, Mr. Roboto gives a few pointers on how to get the most out of it -- starting with Windows Management Instrumentation.

I know it's cliché, but I'm a big fan of teaching others "how to fish" so they can take care of themselves. Mr. Roboto will often offer suggestions and tools to help you get the job done in an efficient and cost-effective manner, but I also want you to know more about how things work so you can develop your own solutions. Because more and more of you will begin using Windows PowerShell -- especially with the arrival of version 2.0 in a few months -- I'll use it as the basis for my scripted solutions.

I can't teach you everything about using Windows PowerShell in a monthly column; in any case, I'm a proponent of learning by doing. The more you understand how something works, the more you see how easy it is. So, on with the fishing lesson.

Make Use of WMI
In Windows PowerShell, Windows Management Instrumentation (WMI) is an amazing way to gather all sorts of information about remote computers. Using the Get-WMIObject cmdlet and a WMI class name, you'll be amazed what you can discover:

PS C:\> get-wmiobject -class 
win32_bios -computername 
FILE02

The challenge is discovering which classes to use. In PowerShell, all you need to do is ask. The Get-WMIObject cmdlet has a -list parameter. The default WMI namespace is Root\CIMv2, which is perfect because 90 percent of your WMI scripting will use the Win32 classes in this namespace. Here's a simple PowerShell expression to display just the Win32 classes on your computer:

PS C:\> Get-wmiobject -list | 
where {$_.name -like "Win32*"} 

Most of the class names should be self-explanatory, but what type of objects will they create? There are a number of free WMI scripts and browsers you can download and use, but I have a short script you can download from Redmondmag.com (see "Roboto on Demand").

Get-WMIInfo.ps1 is designed to display the help description for a given class, along with the class' properties and methods. The script will write a custom object with WMI information to the pipeline, or you can use -report to display an expanded view of the class' properties and methods. The script can also connect to a remote computer. Be aware that the script can't use alternate credentials to connect to remote computers: You must run it under an account that has administrator privileges on the remote computer.

PS C:\Scripts\.\get-wmiinfo.ps1 
-computername FILE01 -class 
win32_diskpartition

This script is designed to give you information about a WMI class so you can later write your own PowerShell scripts and expressions. The script can accept several parameters.

Param([string]$Class="Win32_
   OperatingSystem",
      [string]$computername=$en
   v:computername,
      [string]$namespace="ROOT\
   CIMV2",
      [switch]$report
     )

I've given most of the parameters' default values. You can either specify values in the same order as the parameter variables, or you can treat the variables as runtime parameters as I did in my example.

The script creates a WMIClass object constructed from the computername, namespace and class:

[WMICLASS]$wmi="\\{0}\
{1}:{2}" -f $computername, 
$namespace,$Class

The -f operator replaces the numbered placeholders with the corresponding value from the comma-separated list on the right. This is a very handy technique for building strings because there's no more complicated concatenation.

I hope you find Get-WMIInfo.ps1 a helpful tool for discovering WMI information. There's a lot more about this topic online at Redmondmag.com, including advice for creating property and method names for WMI classes.

Roboto on Demand

To download Mr. Roboto's Get-WMIInfo.ps1 script and learn about creating and displaying properties and methods, click here.

About the Author

Jeffery Hicks is a multi-year Microsoft MVP in Windows PowerShell, Microsoft Certified Professional and an IT veteran with almost 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He works today as an independent author, trainer and consultant. Jeff is a regular contributor to a variety on online sites, as well as frequent speaker at technology conferences and user groups. Keep up with Jeff and his projects at http://jdhitsolutions.com/blog.

comments powered by Disqus
Upcoming Events

Redmond Tech Watch

Sign up for our newsletter.

I agree to this site's Privacy Policy.