Guy’s Scripting Ezine 94 – What WMI Can Do for You

Contents for Ezine 94 – What WMI can do for You


This Week’s Secret

Can I write an ebook on WMI (Windows Management Instrumentation) in a week?  Given my other commitments, probably not.  Can I write a WMI ebook in a fortnight?  Watch this space!  I will give it a try.

What WMI Can Do for You

This week I want to help you get started with WMI.  The keyword in the acronym WMI is instrumentation.  Microsoft provide us with all the tools, objects and methods, it is up to you and I to make best use of these instruments.  Now for the key questions.

Why, is always a good question.  Why write WMI scripts?   The answer is because you want information or control.  For instance, the ultimate skill with Group Policies is to apply WMI filters so that only machines, which match your specifications, get the policy.  By specification I mean, Disk space, memory or operating system.

Another reason for writing WMI is scripts is that you require hardware inventories.  For example, how many of our computers chassis are Desktops and how many are ‘Pizza Boxes’? What are the cooling fans’ specifications on the servers?  Who are the disk manufacturers of our servers in the branch office?  How much L2 Cache does our CPU have?

Perhaps the best question of all is, what can we do to harness the power of WMI?  The answer to that is either create a do-it-yourself script, or call for some level of help.  Scriptomatic is a good place to start, it’s free and Scriptomatic is the script engine of more expensive tools.  If you have already invested in the OnScript editor, check out their free Scriptomatic add-on.  If you have not bought OnScript then I recommend that you get a free trial of this wonderful script editor.

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

WMI Methods and Mission

Whereas logon scripts have only one object (network) and a handful of methods, for example, MapNetworkDrive and AddPrinterConnection, WMI has tens of objects and hundreds of methods.

The idea behind this week’s VBScript is to interrogate the operating system and discover the chassis type.  I have to admit that previously, I did not realize the variety of different chassis types.  Not only are there Desktop and Laptop, but also ‘Pizza Box’ and ‘Lunch Box’. However, my real mission is to introduce to the WMI components.  Please realize that you could substitute any of the WMI classes below for Win32_SystemEnclosure.  I admit that you would need Scriptomatic to identify the correct properties for whichever class you choose.

Obvious Counters.

  1. Win32_PhysicalMemory – not Win32_Memory.
  2. Win32_Processor – Clock Speed, Voltage, Cache size
  3. Win32_DiskDrive – Physical Disk data. Based on the number of blocks, tracks or sectors.
  4. Win32_LogicalDisk – Partition size, Drive Type and FreeSpace.

More WMI Counters

Many of these WMI Classes need a filter for example: 
For Each objChassis in colChassis
  For Each objItem in objChassis.ChassisTypes
Without a filter you get swamped with data.  However, I want to begin by giving you the broadest possible perspective, so here is a list of useful classes, each with ten or more properties.

  1. Win32_SystemEnclosure – What type of chassis does your computer have?  See this week’s script.
  2. Win32_Keyboard – Worth a look.
  3. Win32_LoadOrderGroup – More services than I anticipated.
  4. Win32_LoggedOnUser – More accounts than I predicted.
  5. Win32_PointingDevice – Reminds me HID means Human Interface Device.
  6. Win32_Product – Check software installed.  Ideas for checking viruses?
  7. Win32_SystemBios – See if there have been upgrades.
  8. Win32_SystemOperatingSystem – Boot partition information.
  9. Win32_TimeZone – Not quite what I thought.  Displays the months, days and hours when the clocks spring forward or fall back.
  10. Win32_VideoSettings – Disappointing.  Displays all possible values rather than current settings.

Instructions for Creating a WMI script to Discover the Chassis Type

  1. Logon as a local Administrator.
  2. Copy and paste the example script below into notepad or use a VBScript editor.
  3. Save the file with a .vbs extension, for example: Chassis.vbs 
  4. Double click and check machine’s chassis type.
  5. Optional suggestion edit strComputer = "." to the NetBIOS name of another computer.

‘ Chassis.vbs
‘ VBScript to interogate a machine’s chassis type.
‘ Author Guy Thomas
‘ Version 2.3 – November 2005
‘ ———————————————————‘
Option Explicit
Dim strComputer, strChassis
Dim objWMIService, objChassis, colChassis, objItem
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colChassis = objWMIService.ExecQuery _
("Select * from Win32_SystemEnclosure",,16)
For Each objChassis in colChassis
  For Each objItem in objChassis.ChassisTypes
    Select Case objItem
    Case 1 strChassis = "Maybe Virtual Machine"
    Case 2 strChassis = "??"
    Case 3 strChassis = "Desktop"
    Case 4 strChassis = "Thin Desktop"
    Case 5 strChassis = "Pizza Box"
    Case 6 strChassis = "Mini Tower"
    Case 7 strChassis = "Full Tower"
    Case 8 strChassis = "Portable"
    Case 9 strChassis = "Laptop"
    Case 10 strChassis = "Notebook"
    Case 11 strChassis = "Hand Held"
    Case 12 strChassis = "Docking Station"
    Case 13 strChassis = "All in One"
    Case 14 strChassis = "Sub Notebook"
    Case 15 strChassis = "Space-Saving"
    Case 16 strChassis = "Lunch Box"
    Case 17 strChassis = "Main System Chassis"
    Case 18 strChassis = "Lunch Box"
    Case 19 strChassis = "SubChassis"
    Case 20 strChassis = "Bus Expansion Chassis"
    Case 21 strChassis = "Peripheral Chassis"
    Case 22 strChassis = "Storage Chassis"
    Case 23 strChassis = "Rack Mount Unit"
    Case 24 strChassis = "Sealed-Case PC"

    End Select
WScript.Echo "Computer chassis type: " & strChassis
‘WScript.Echo strComputer & "’s chassis type: " & strChassis


‘ End of WMI VBScript – Chassis type

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.

Learning Points

Note 1:  Consider this section of the script.

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")

The above phrase is standard in so many WMI scripts.  What it says in plain English is, connect to the heart of the WMI namespace.  {impersonationLevel=impersonate} is one of those commands that I just accept is necessary.  In fact, it reminds me to logon as an administrator so that the script will not fail for the trivial reason of insufficient user rights.

Note 2:  Set colChassis = objWMIService.ExecQuery _
("Select * from Win32_SystemEnclosure",,16)

As ever, note the verbs.  Firstly, ExecQuery which says carry out the request.  Select * is famous in all query languages, get all. 

Note 3:  Identify the class – Win32_SystemEnclosure.  The Win32_xyz class is the first place that I look to see the purpose of a particular script.  SystemEnclosure means the computer ‘Box’, ‘Unit’, the chassis type.

Note 4: This script employs two loops.  The phrase ‘col’ means collation.  WMI scripts interrogate a whole collection of properties, for this example I have singled out objChassis.ChassisTypes.

Note 5:  I never miss an opportunity to showcase the ‘Select Case’ construction.  Multiple ‘Else If’ would be ungainly, whereas Select Case works elegantly.

Note 6:  Edit strComputer = "." and substitute the name of another machine on your network.  If you accept this challenge, uncomment the last but 3 line.

Summary of WMI

Remember that the operating system knows everything.  Thank Microsoft for providing a library of WMI commands with which to interrogate systems like the Event Logs.  WMI scripting is tricky because there are so many elements.  Take the time to have a walk through of how you examine an Event Log property sheet.  In particular, match the Event ID, Success or Failure and type of Log with variables in my script.

See More WMI Scripts

• WMI  • Ezines  • WMI Basics   • WMI PowerShell  • Free SolarWinds WMI Monitor

Ezine 9 WMI  • Ezine 10 WMI  • Ezine 19 WMI  • Ezine 40 Variables  • Ezine 48 WMI 

Ezine 52 WMI OS  • Ezine 76 WMI Classes  • Ezine 93 WMI  • Ezine 94 WMI  • Ezine 95 WMI

Ezine 110 WMI PowerShell  •Ezine 114 WMI Path  • Tool Kit