Guy’s Scripting Ezine 76 –  Introduction to WMI

Contents for Ezine 76 Introduction to WMI

 ♣

This Week’s Secret

Guy’s third law of computing says, ‘The more acronyms on a page, the harder the topic is to understand’. On that score WMI comes out medium difficult.

WMI stands for Windows Management Instrumentation. What WMI delivers is control through scripting.  Another way of looking at WMI is a variation of VBScript, which allows you to measure and automate every aspect of computer behaviour.  The key acronym is CIM, Common Information Model. All that you need to know about CIM is that it is an open standard for defining every conceivable computer object, from the BIOS to Disks, Memory even the Desktop.

My secrets for getting started with WMI are as follows, look for patterns, especially with Win32_Xyz objects where Xyz could be Process, Service, DiskDrive or Memory.  As with all VBScripts, build the script in stages, and then bolt the parts together. You probably need no encouragement to learn by copying and pasting other people’s scripts. Of all the computing tasks, understanding WMI is particularly suited to the attitude: just learn by doing.

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.

Shell One –  A Simple VBScript

Learning WMI is not going to be easy.  The reason that WMI is so difficult is that you have to master not only the core WMI commands, but also the ‘wrapper’ VBScript commands.  To get you started, I have developed what I call the three shell method.  Traditional WMI scripts can be long and complex, therefore, I am going to strip the code down to the essentials, get success, then gradually build in more layers, or shells.

Remember, that our mission for Shell One is making sure that the VBScript shell or ‘wrapper’ is ready to receive WMI commands.  Shell One is concerned with only VBScript; there are no WMI commands at this stage.

Instructions for Creating your Shell One VBScript.

  1. Copy and paste the example script below into notepad or a VBScript editor.
  2. Save the file with a .vbs extension, for example: ShellOne.vbs. 
  3. Double click ShellOne.vbs and check the message box.

Guy Recommends: The Free IP Address Tracker (IPAT) IP Tracker

Calculating IP Address ranges is a black art, which many network managers solve by creating custom Excel spreadsheets.  IPAT cracks this problem of allocating IP addresses in networks in two ways:

For Mr Organized there is a nifty subnet calculator, you enter the network address and the subnet mask, then IPAT works out the usable addresses and their ranges. 

For Mr Lazy IPAT discovers and then displays the IP addresses of existing computers. Download the Free IP Address Tracker

Shell One – VBScript

 

 ‘ ShellOne.vbs
‘ Sample VBScript – Shell one
‘ Author Guy Thomas https://computerperformance.co.uk/
‘ Version 1.2 – June 2005
‘ —————————————————-‘
Option Explicit
Dim strComputer

strComputer = "CheckMe"
WScript.Echo "Computer: " & strComputer
WScript.Quit

‘ End of Sample VBScript to Echo Computer Name

Learning Points for Shell One

1) My goal for Shell One is simply to isolate the VBScript commands.

2) If this script works, then it proves that your computer can interpret VBScript and is ready for WMI commands.

3) In Shell One the only real instruction is WScript.Echo.  The purpose of the rest of the script is just to introduce you to my headers, Dim statement and strComputer.

Guy Recommends: The Free IP Address Tracker (IPAT) IP Tracker

Calculating IP Address ranges is a black art, which many network managers solve by creating custom Excel spreadsheets.  IPAT cracks this problem of allocating IP addresses in networks in two ways:

For Mr Organized there is a nifty subnet calculator, you enter the network address and the subnet mask, then IPAT works out the usable addresses and their ranges. 

For Mr Lazy IPAT discovers and then displays the IP addresses of existing computers. Download the Free IP Address Tracker

Shell Two – A Template for WMI Commands

Now we are ready to add a simple WMI core to the VBScript in Shell One.  The result will be a template to use for future, more complex WMI scripts.  As far as I know, all the Win32_Objects have these 3 properties, .name, .caption and .description.  Therefore, we could choose any Win32 object on line 16.  As it happens, from all the possible objects, I selected Win32_Bus.

Instructions for Creating your WMI Template

  1. Copy and paste the example script below into notepad or a VBScript editor.
  2. Begin with strComputer = "."  Once the script works substitute the name of another machine on the network.
  3. Save the file with a .vbs extension, for example: ShellTwo.vbs (or Template.vbs)
  4. Double click ShellTwo.vbs and check the Bus information in the message box.

Shell Two – The Template for WMI Commands

 

 

‘ ShellTwo.vbs
‘ Template for a WMI script to interrogate Win32_Objects
‘ Author Guy Thomas https://computerperformance.co.uk/
‘ Version 2.2 – June 2005
‘ ——————————————————–‘
Option Explicit
Dim objWMIService, objItem, colItems, strComputer

‘ On Error Resume Next
strComputer = "."

‘ WMI Core commands to connect to object database
‘ ============================================
Set objWMIService = GetObject("winmgmts:\\" _
& strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery(_
"Select * from Win32_Bus")

‘ VBScript loop containing WMI Properties
‘ ============================================
For Each objItem in colItems
Wscript.Echo "Computer " & strComputer & vbCr & _
"Caption: " & objItem.Caption & VbCr & _
"Description: " & objItem.Description & VbCr & _
"Name: " & objItem.Name & VbCr & _
" (More Properties in production script)"& VbCr & _
""
Next
WSCript.Quit

‘ End of Template WMI Sample VBScript

WMI Tutorial Learning Points

1)  Let us focus on the WMI core statements:

a) Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2").  This command instructs winmgmts to connect to the heart of the namespace, root\cimv2

b) Set colItems = objWMIService.ExecQuery( "Select * from Win32_Bus").  Once the previous line connects to CIM, this line executes a WQL query, Select everything from the Bus.

2)  Next, it’s back to VBScript for a For …. Next loop.  Incidentally, all the scripts that we create from this template will use the same loop structure.  This Shell Two script has only three very basic (and boring) properties, future scripts will add more interesting properties.  Every object has a name attribute, hence, objItem.Name.  To my knowledge, all, Win32 objects also have the .Description and .Caption properties.

3) There are three pieces of VBScript syntax that you should master, as a missing ampersand, VbCr or underscore can wreak havoc with more complex WMI scripts.

a) _(Underscore) is the equivalent of word-wrap.  Without this tiny character, VBScript thinks that the next line is a completely different command.  With the underscore, you can string together a dozen or more properties.  Just remember that there is no need for underscore after the last description.  To let you into a secret, this is why I end with a blank line "", to remind me no more underscores.

b) The & (ampersand) is my Achilles’ heel.  I know & means append or join two pieces of text, but sometimes forget, only for the script to remind me with an 800A0401 – ‘Expected end of statement’ error message.

c) VbCr means a visual basic carriage return. Perhaps you are thinking, we already covered this with a) Underscore.  No. VbCr is for line breaks in the output, whereas underscore is for the interpretation of the very script.  Put another way, the script will work without any VbCr commands, but it will be exceedingly awkward to read.

Shell Three – Your working WMI Script

Now that we have a template, it’s like shelling peas.  Decide on the object’s properties that you need, and then just cut and paste them into Shell Two.  The result will be a useful production script.

There are only points to watch.

1) Remember to change the Win32_object to match the properties, for example change,
"Select * from Win32_Bus") to "Select * from Win32_ComputerSystem").  Failure to edit Win32_Xyx results in error message – 800A01B6 Object doesn’t support this property.

2) Select the Win32 object’s properties from your own research or from my table below.  Here is an example where I have added extra properties on lines 23-31 of Shell Three.

 

 

‘ ComputerSystem.vbs
‘ Sample WMI script to interrogate Win32_ComputerSystem
‘ Author Guy Thomas https://computerperformance.co.uk/
‘ Version 3.1 – June 2005
‘ ————————————————————–‘
Option Explicit
Dim objWMIService, objItem, colItems, strComputer

‘ On Error Resume Next
strComputer = "LocalHost"

Set objWMIService = GetObject("winmgmts:\\" _
& strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery(_
"Select * from Win32_ComputerSystem")

For Each objItem in colItems
Wscript.Echo "Computer " & strComputer & vbCr & _
"Caption: " & objItem.Caption & VbCr & _
"Description: " & objItem.Description & VbCr & _
"Name: " & objItem.Name & VbCr & _
""& VbCr & _
"UserName: " & objItem.UserName & vbCr & _
"DNSHostName: " & objItem.DNSHostName & vbCr & _
"Domain: " & objItem.Domain & vbCr & _
"Manufacturer: " & objItem.Manufacturer & vbCr & _
"Model: " & objItem.Model & vbCr & _
"NumberOfProcessors: " & objItem.NumberOfProcessors & vbCr & _
"PartOfDomain: " & objItem.PartOfDomain & vbCr & _
"ChassisBootup: " & objItem.ChassisBootupState & vbCr & _
"TotalRamMemory: " & objItem.TotalPhysicalMemory & vbCr & _
""
Next
WSCript.Quit

‘ End of WMI ComputerSystem Sample VBScript

Selection of Win32 Objects and Properties

Employ Shell Two as your template.  Copy and paste different objects, but make sure that choose properties that correspond to the correct object.  Here is the key, change the object in line 16 in Shell Two (Template) : for example,
"Select * from Win32_Bus") amend to read, "Select * from Win32_ComputerSystem")

 Win32_Object

Properties to Add to the Shell

 Win32_BIOS "BuildNumber: " & objItem.BuildNumber & vbCr & _
"Manufacturer: " & objItem.Manufacturer & VbCr & _
"PrimaryBIOS: " & objItem.PrimaryBIOS & vbCr & _
"SMBIOSBIOSVersion: " & objItem.SMBIOSBIOSVersion & vbCr & _
"SMBIOSMajorVersion: " & objItem.SMBIOSMajorVersion & vbCr & _
"SMBIOSMinorVersion: " & objItem.SMBIOSMinorVersion & vbCr & _
"SMBIOSPresent: " & objItem.SMBIOSPresent & vbCr & _
"SoftwareElementID: " & objItem.SoftwareElementID & vbCr & _
"TargetOperatingSystem: " & objItem.TargetOperatingSystem & vbCr & _
"OtherTargetOS: " & objItem.OtherTargetOS & vbCr & _
Win32_BootConfiguration "BootDirectory: " & objItem.BootDirectory & VbCr & _
"ConfigurationPath: " & objItem.ConfigurationPath & VbCr & _
"LastDrive: " & objItem.LastDrive & VbCr & _
Win32_Bus "BusNum: " & objItem.BusNum & vbCr & _
"BusType: " & objItem.BusType & vbCr & _
"CreationClassName: " & objItem.CreationClassName & vbCr & _
"DeviceID: " & objItem.DeviceID & vbCr & _
"PNPDeviceID: " & objItem.PNPDeviceID & vbCr & _
"SystemName: " & objItem.SystemName & vbCr & _
Win32_Desktop "CoolSwitch: (Windows + d) " & objItem.CoolSwitch & vbCr & _
"CursorBlinkRate: " & objItem.CursorBlinkRate & vbCr & _
"DragFullWindows: " & objItem.DragFullWindows & vbCr & _
"GridGranularity: " & objItem.GridGranularity & vbCr & _
"IconSpacing: " & objItem.IconSpacing & vbCr & _
"IconTitleFaceName: " & objItem.IconTitleFaceName & vbCr & _
"IconTitleSize: " & objItem.IconTitleSize & vbCr & _
"IconTitleWrap: " & objItem.IconTitleWrap & vbCr & _
"Pattern: " & objItem.Pattern & vbCr & _
"ScreenSaverActive: " & objItem.ScreenSaverActive & vbCr & _
"ScreenSaverExecutable: " & objItem.ScreenSaverExecutable & vbCr & _
"ScreenSaverSecure: " & objItem.ScreenSaverSecure & vbCr & _
"ScreenSaverTimeout: " & objItem.ScreenSaverTimeout & vbCr & _
"SettingID: " & objItem.SettingID & vbCr & _
"Wallpaper: " & objItem.Wallpaper & vbCr & _
Win32_ComputerSystems "AdminPasswordStatus: " & objItem.AdminPasswordStatus & vbCr & _
"AutoResetBootOption: " & objItem.AutomaticResetBootOption & vbCr & _
"AutomaticResetCapability: " & objItem.AutomaticResetCapability & vbCr & _
"BootROMSupported: " & objItem.BootROMSupported & vbCr & _
"BootupState: " & objItem.BootupState & vbCr & _
"Caption: " & objItem.Caption & vbCr & _
"ChassisBootupState: " & objItem.ChassisBootupState & vbCr & _
"CreationClassName: " & objItem.CreationClassName & vbCr & _
"CurrentTimeZone: " & objItem.CurrentTimeZone & vbCr & _
"DaylightInEffect: " & objItem.DaylightInEffect & vbCr & _
"Description: " & objItem.Description & vbCr & _
"DNSHostName: " & objItem.DNSHostName & vbCr & _
"Domain: " & objItem.Domain & vbCr & _
"DomainRole: " & objItem.DomainRole & vbCr & _
"FrontPanelResetStatus: " & objItem.FrontPanelResetStatus & vbCr & _
"InfraredSupported: " & objItem.InfraredSupported & vbCr & _
"InitialLoadInfo: " & objItem.InitialLoadInfo & vbCr & _
"InstallDate: " & objItem.InstallDate & vbCr & _
"KeyboardPasswordStatus: " & objItem.KeyboardPasswordStatus & vbCr & _
"LastLoadInfo: " & objItem.LastLoadInfo & vbCr & _
"Manufacturer: " & objItem.Manufacturer & vbCr & _
"Model: " & objItem.Model & vbCr & _
"NetworkServerMode: " & objItem.NetworkServerModeEnabled & vbCr & _
"NumberOfProcessors: " & objItem.NumberOfProcessors & vbCr & _
"PartOfDomain: " & objItem.PartOfDomain & vbCr & _
"PauseAfterReset: " & objItem.PauseAfterReset & vbCr & _
"PowerMgmentCpbilit: " & objItem.PowerManagementCapabilities & vbCr & _
"PowerMgementSupport: " & objItem.PowerManagementSupported & vbCr & _
"PowerOnPasswordStatus: " & objItem.PowerOnPasswordStatus & vbCr & _
"PowerState: " & objItem.PowerState & vbCr & _
"PowerSupplyState: " & objItem.PowerSupplyState & vbCr & _
"TotalPhysicalMemory: " & objItem.TotalPhysicalMemory & vbCr & _
"UserName: " & objItem.UserName & vbCr & _

WMI Tip  For much more on WMI, see my WMI Section here.

Summary of Getting Started with WMI

I love to build on success.  Even if you go back to basics and start with a simple script, it’s worth it for that feeling of achievement, that belief that you are on a roll and can tackle anything.  In practical terms, I hope that you will identify the key Win32 commands, and see what I mean by a VBScript shell with a WMI core.

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