Contents for Ezine 76 Introduction to WMI
♣ This Week’s SecretGuy’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 UMRATired 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 VBScriptLearning 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.
Guy Recommends: The Free IP Address Tracker (IPAT)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 strComputer = "CheckMe" ‘ End of Sample VBScript to Echo Computer Name Learning Points for Shell One1) 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)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 CommandsNow 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
Shell Two – The Template for WMI Commands
‘ ShellTwo.vbs ‘ On Error Resume Next ‘ WMI Core commands to connect to object database ‘ VBScript loop containing WMI Properties ‘ End of Template WMI Sample VBScript WMI Tutorial Learning Points1) 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 ScriptNow 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, 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 ‘ On Error Resume Next Set objWMIService = GetObject("winmgmts:\\" _ For Each objItem in colItems ‘ End of WMI ComputerSystem Sample VBScript Selection of Win32 Objects and PropertiesEmploy 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,
For much more on WMI, see my WMI Section here. Summary of Getting Started with WMII 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 |