WMI (Windows Management Instrumentation) – Getting Started

Getting Started with WMI

This page will set you on your way to creating VBScripts with WMI queries.  I know that you love to copy example scripts, paste them into notepad, amend a few lines and then run them.  Therfore, 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.

Topics for Getting Started with WMI

 ♣

What to Look for in a WMI Script

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

Shell One –  A Simple VBScript

Mastering WMI is not going to be easy.  The reason that WMI is so difficult is you have to understand 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 build layers, or shells gradually.

Our mission for Shell One is very simple, to make sure that the VBScript shell or ‘wrapper’ is ready to receive the core WMI commands.  Shell One is concerned with only VBScript; there are no WMI commands at this stage.  Stage one is like just starting the engine.  We are not going anywhere just yet, merely ticking over with the handbrake on.

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.

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

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

‘ End of Sample VBScript to Echo Computer Name

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

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.

Shell Two – A Template for WMI Commands

Now we are ready to get moving and add a simple WMI engine to the VBScript in Shell One.  The result will be a template for use in more complex WMI scripts.  As far as I know, all Win32_Objects have these 3 properties, .name, .caption and .description.  Therefore, we could choose any Win32 object on line 16.  With all this talk of getting into gear and engines, from all the possible objects, it amused me to select the Win32_Bus as the vehicle for this example.

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 your 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.4 – November 2010
‘ ——————————————————–‘
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 & _
"Name   : " & objItem.Name & VbCr & _
"Description  : " & objItem.Description & VbCr & _
"DeviceID     : " & objItem.DeviceID & VbCr & _
"PNPDeviceID: " & objItem.PNPDeviceID & VbCr & _
" (More Properties in production script)"& VbCr & _
""
Next
WSCript.Quit

‘ End of Template WMI Sample VBScript

Learning Points for Shell Two

1)  Because they occur in all WMI scripts, let us focus on the two WMI core statements:

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

b) Line 14: 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 Win32_Bus.

2)  Next, it’s back to VBScript because we need a loop, see For …. Next… loop.  Actually we can see on Line 17 that we need For Each, rather than plain For.  I mention this because here is where VBScript meshes with WMI commands.

Incidentally, all the scripts that we create from this template will use the same loop structure.  This Shell Two script has only very basic 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) Here are three pieces of pure VBScript syntax that you should master: & (ampersand), VbCr and _ (underscore).  Get any of these tiny commands wrong  and it can mean hours of troubleshooting in a more complex script.

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 two pieces of text, but sometimes I 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.

Guy Recommends SolarWinds’ Free Network MonitorSolarwinds Network Device Monitor

Thus utility makes it easy to check the health of a router or firewall.  Check the real-time performance, and availability statistics, for any device on your network.  Get started with an extensive collection of "out-of-the-box" monitors for popular network devices. Give Network Monitor a whirl – it’s free. Download your free Network Device Monitor

If you need more comprehensive network analysis software:
Download a free trial of NPM (Network Performance Monitor)

Shell Three – Your Working WMI Script

Now that we have a template, we can create a production line for WMI scripts.  The tool that makes an ordinary mortal a genius is Scriptomatic.   With Scriptomatic you can investigate each Object and all the associated properties.  Once you have finished looking at Scriptomatic’s menus, for each WMI script, firstly decide on the object.  Shell Two above, uses "Select * from Win32_Bus").  In Shell Three below, I chose, to substitute Win32_ComputerSystem.  Secondly decide on the properties, for example, objItem.TotalPhysicalMemory.

There are only two further 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 with Scriptomatic 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 – November 2010
‘ ———————————————–‘
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

Solarwinds Config GeneratorGuy Recommends: The Free Config Generator

SolarWinds’ Config Generator is a free tool, which puts you in charge of controlling changes to network routers and other SNMP devices.  Boost your network performance by activating network device features you’ve already paid for.

Guy says that for newbies the biggest benefit of this free tool is that it will provide the impetus for you to learn more about configuring the SNMP service with its ‘Traps’ and ‘Communities’. Try Config Generator now – it’s free!

Download your free copy of Config Generator

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 & _

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 returning to the beginning just to get 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 realize what I mean by a VBScript shell with a WMI core.

See Also

WMI Home    • Simple WMI Script      • Start / Stop Services