WMI (Windows Management Instrumentation) - Secrets
WMI and VBScript Secrets
Here are my tips on mastering WMI. Start with a simple script. My advice is always aim to build on success. The knack of scripting is to divide your task into sections, in our case, pure VBScript, WMI and CIMv2. Get each command working then bolt the sections to make your production WMI script.
Topics for WMI and VBScript Secrets
We have a simple script to get started, our mission is to master basic VBScript commands. Following the plan I outlined early, let us break the script down into sections. Firstly the pure VBScript, secondly to add in the WMI components.
This is a very basic script, which fits with my aim to start simply.
This script is suitable for most clients, XP, W2K, even NT 4.0 and Win 9x.
Instructions for Creating your WMI Script
Script to Demonstrate the Basics of VBScript
1) This script deliberately has no WMI commands. In Example 1, I just want to focus on the minimal VBScript commands which provide the wrappers for the WMI section. WMI itself, is added in Example 2.
2) WScript is a built in executable for Windows 2000 and later machines. Although WScript operates through 20 or more methods, in this example we employ just 2 WScript methods; the .Echo and .Quit.
3) Even though this is a short script, I still like to create a header. In this top section, or header, I set out the purpose and then declare the one variable
4) Although not strictly necessary, this script introduces the underscore (_) in line 11. My message is learn this basic VBScript syntax in a simple script, then apply the syntax smoothly to the production versions. Underscore says to VBScript, 'here is where we word-wrap'. Almost every WMI script requires an _. You could just accept that without the underscore you get an error. Better still you could appreciate that where commands are too long to fit on one line, we need a marker to tell VBScript to carryon reading the command on the next line.
5) To be honest, this first script does not do much, but it gives a grounding in VBScript. My idea was to prepare you for the WMI in Example 2. If you were slightly disappointed with the output, I have a more advanced script to echo the machine's actual hostname.
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.
Example 1b: To Discover the ComputerName
While this script is more satisfying, Example 1b is a digression on our main task of mastering WMI.
This script not only demonstrates the basics of WMI, but it also has a practical goal of querying a machine for information about the operating system.
Script to Query the Computer for Operating System Details
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!
1) Let us concentrate on the WMI commands on lines 13-19.
From example 1a we learnt that the _ (underscore) is solely for word-wrap. If there is
room on the line, you could edit to:
Next, I would like to dissect the above command into 4 parts.
a) The goal of this command is to get a handle on the CIM namespace \root\cimv2.
b) GetObject("winmgmts: means call for the winmgmts service to connect to the CIM namespace.
c) strComputer is the variable that we declared earlier. We could experiment and set strComputer = "othermachine" on line 11.
d) Set objWMIService creates a new variable, or place holder so that we can reuse the \root\cimv2 object later in the script.
2) Set colItems = objWMIService.ExecQuery
a) If you remember we just created the variable objWMIService. Now we are going to put it the ExecQuery method use and execute a WQL command.
b) ("Select * from Win32_OperatingSystem") is a classic database SQL or WQL command. The key feature is Win32_OperatingSystem. As you get to know WMI scripts, so you will use many different Win32_objects, for example Win32_Process, Win32_PhysicalDisk. Well, in this instance it is the operating system that we are interested in.
c) See how all the above information is transferred to a new variable colItems.
3) Here is the section where WMI commands link to VBScript commands. VBScript provides the loop with. 'For Each ... In.....Next', and WMI provides the objects to interrogate with objItem and colItems.
4) Each line (apart from the last) ends with & VbCr & _. What this VBScript command does is tell the script that there are more instructions on the next line, the _ really comes into its own here. The vbCr provides a line break in the output, not in the script. See in the next section tips on how to add & VbCr & _ to your scripts.
The biggest problem that people have when they write to me is that their scripts are too complex. Learn from this and create short scripts. Even just write snippets. Separate out the pure WMI from the VBScript wrapper, get each part working, then copy and paste into the final script.
If you like this page then please share it with your friends
See more VBScript WMI examples:
Windows Management Instrumentation (WMI) is most useful for PowerShell scripting.
SolarWinds have produced this Free WMI Monitor to take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.
Author: Guy Thomas Copyright © 1999-2017 Computer Performance LTD All rights reserved.
Please report a broken link, or an error to: