Contents for Ezine 94 - What WMI can do for You
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. 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.
If you are looking for handy network utilities, try some of the free downloads at
Tools4Ever
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. - Win32_PhysicalMemory - not Win32_Memory.
- Win32_Processor - Clock Speed, Voltage, Cache size
- Win32_DiskDrive - Physical Disk data.
Based on the number of blocks, tracks or sectors.
- 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. - Win32_SystemEnclosure - What type of chassis does your computer have? See this week's script.
- Win32_Keyboard - Worth a look.
- Win32_LoadOrderGroup - More services than I anticipated.
- Win32_LoggedOnUser - More accounts than I predicted.
- Win32_PointingDevice - Reminds me HID
means Human Interface Device.
- Win32_Product - Check software installed. Ideas for checking viruses?
- Win32_SystemBios - See if there have been upgrades.
- Win32_SystemOperatingSystem -
Boot partition information.
- Win32_TimeZone - Not quite what I thought. Displays the months, days and hours when the clocks spring forward or fall back.
- Win32_VideoSettings - Disappointing.
Displays all possible values rather than current settings.
Instructions for Creating a WMI script to Discover the Chassis Type
- Logon as a local Administrator.
- Copy and paste the example script below into notepad or
use a VBScript editor.
- Save the file with a .vbs extension, for example: Chassis.vbs
- Double click and check machine's chassis type.
- Optional suggestion edit strComputer = "."
to the NetBIOS name of another computer.
' Chassis.vbs ' VBScript to interogate a machine's chassis type. ' Author Guy Thomas http://computerperformance.co.uk/ ' 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 Next Next
WScript.Echo "Computer chassis type: " & strChassis 'WScript.Echo strComputer & "'s chassis type: " & strChassis WScript.Quit ' End of WMI VBScript - Chassis type
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. 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.
Their topics and material are ideal for getting you started with VBScript. The
videos are easy to follow and you can control the pace. Try their free demo material and then see if you want to buy the full package.
See more about VB Script Training CD.
|