http://www.computerperformance.co.uk/vbscript/wmi_secrets.htm
http://www.computerperformance.co.uk/vbscript/wmi_technique.htm
Introduction to WMI SecretsHere 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 Secrets
Getting started with WMIWe 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. Example 1a: To Echo the Name Set by the strComputer VariableThis is a very basic script, which fits with my aim to start simply. Prerequisites 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.
' Simple1.vbs VBScript Learning Points in Preparation for WMI1) 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 with: 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. Example 1b: To Discover the ComputerNameWhile this script is more satisfying, Example 1b is a digression on our main task of mastering WMI. ' ComputerName.vbs
Example 2 - WMI Script to Obtain Information about the Operating SystemThis 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 ' OperatingSystem.vbs WMI Tutorial Learning Points1) 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. Bonus Technique - Make the script easier to read. Summary of WMI SecretsThe 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. |