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.
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.
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
Copy and paste the example script below into notepad or a VBScript editor.
Save the file with a .vbs extension, for example: ShellOne.vbs.
Double click ShellOne.vbs and check the message box.
' ShellOne.vbs ' Sample VBScript - Shell one ' Author Guy Thomas http://computerperformance.co.uk/ ' Version 1.2 - December 2010 '
----------------------------------------------------' Option Explicit Dim strComputer
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft 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.
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.
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
Copy and paste the example script below into notepad or a VBScript editor.
Begin with strComputer = "." Once the script works substitute the name of another machine on your network.
Save the file with a .vbs extension, for example: ShellTwo.vbs (or Template.vbs)
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 http://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")
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.
Thus utility makes it easy to check the health of your 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.
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.
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'.
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")
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.
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft operating systems.
Fortunately, SolarWinds
have created the
Free WMI Monitor so that you can actually see and understand these gems of
performance information. Take the guess work out of which
WMI counters to use for applications like Microsoft Active Directory,
SQL or Exchange Server.