WMI Scripts

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

 ♦

Getting Started with WMI

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.

Example 1a: To Echo the Name Set by the strComputer Variable

This 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

  1. Copy and paste the example script below into notepad or a VBScript editor.
  2. Decide the name of the machine on line 8.
  3. Save the file with a .vbs extension, for example: Simple1.vbs.
  4. Double click Simple1.vbs and check the UserName.

Script to Demonstrate the Basics of VBScript

' Simple1.vbs
' Sample VBScript to display the ComputerName
' Author Guy Thomas http://computerperformance.co.uk/
' Version 1.5 - November 2010
' --------------------------------------------------'
Option Explicit
Dim strComputer
strComputer = "LocalHost"
WScript.Echo "Computer: " _
& strComputer
WScript.Quit
' End of VBScript example.

VBScript Learning Points in Preparation for WMI

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 with:
Dim strComputer

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.

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

Example 1b: To Discover the ComputerName

While this script is more satisfying, Example 1b is a digression on our main task of mastering WMI.

' ComputerName.vbs
' Sample VBScript to display the ComputerName
' Author Guy Thomas http://computerperformance.co.uk/
' Version 1.8 - November 2010
' ----------------------------------------------------'
Option Explicit
Dim strComputer, objNetwork

Set objNetwork = WScript.CreateObject("WScript.Network")
WScript.Echo "Computer = " & objNetwork.ComputerName
WScript.Quit

' End of VBScript example.

Note: See more on using WMI Monitor.

Example 2 - WMI VBScript to Obtain Information About the Operating System

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

' OperatingSystem.vbs
' VBScript WMI to document your Operating System
' Author Guy Thomas http://computerperformance.co.uk/
' Version 1.4 - November 2010
' -------------------------------------------------------'
Option Explicit
Dim objWMIService, objItem, colItems
Dim strComputer, strList

On Error Resume Next
strComputer = "."

' WMI Connection to the object in the CIM namespace
Set objWMIService = GetObject("winmgmts:\\" _
& strComputer & "\root\cimv2")

' WMI Query to the Win32_OperatingSystem
Set colItems = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")

' For Each... In Loop (Next at the very end)
For Each objItem in colItems
WScript.Echo "Machine Name: " & objItem.CSName & VbCr & _
"===================================" & vbCr & _
"Processor: " & objItem.Description & VbCr & _
"Manufacturer: " & objItem.Manufacturer & VbCr & _
"Operating System: " & objItem.Caption & VbCr & _
"Version: " & objItem.Version & VbCr & _
"Service Pack: " & objItem.CSDVersion & VbCr & _
"CodeSet: " & objItem.CodeSet & VbCr & _
"CountryCode: " & objItem.CountryCode & VbCr & _
"OSLanguage: " & objItem.OSLanguage & VbCr & _
"CurrentTimeZone: " & objItem.CurrentTimeZone & VbCr & _
"Locale: " & objItem.Locale & VbCr & _
"SerialNumber: " & objItem.SerialNumber & VbCr & _
"SystemDrive: " & objItem.SystemDrive & VbCr & _
"WindowsDirectory: " & objItem.WindowsDirectory & VbCr & _
""
Next
WSCript.Quit

' End of WMI Win32_OperatingSystem 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

WMI VBScript - Learning Points

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:
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

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
("Select * from Win32_OperatingSystem")

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 Secrets

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:

WMI Tutorial   • WMI Who Logged On   • WMI WBEMTest   • Free WMI Monitor   • Free WMI Monitor

WMI Secrets   • VBScript Services   • WMI Techniques   • WMI Scriptomatic

WMI Home   • WMI Moniker   • Import CSVDE - Free Utility   • VBScript Echo   • WMI VBScript

 *


Custom Search

Site Home

Guy Recommends: WMI Monitor for PowershellSolarwinds WMI Monitor

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.

Download your free copy of WMI Monitor

Author: Guy Thomas Copyright © 1999-2014 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: