Guy’s Scripting Ezine 10 – WMI Part 2

Contents of Guy’s Scripting Ezine No 10 – WMI Part 2

Note, See more on WMI a whole section here

Why Use WMI Scripts?

Firstly, a reminder of the benefits of becoming an expert in WMI scripting.  The situation is that you would like to see instant data on your operating system processes.  A VBScript would be useful to save those time consuming tasks of sifting through log files.  A server technician needs skills from many different areas, learning WMI scripting will help make you an expert on Windows Server 2003.

This Week’s Secret

This week I want to show you a hidden treasure in the form of WBEMTEST.EXE (WMI Tester).  This weeks mission is to use this scripting tool to discover the names of  objects that we can use in our WMI scripts.  For example, you may want a script to tell you the free space on each of your disks.  By using WBEMTEST we won’t have to guess, the tester will give us the precise spelling.

WBEMTEST a built-in utility found in Windows 2000, Windows Server 2003 and surprisingly, its there in XP.  (Another surprise is that WBEM stands for Web Based Enterprise Management.)  If you are on a different operating system, you can

WBMEMTEST.EXE

To see the tool, just go the Start (Button), Run, then type – WBEMTEST. 

A reminder of our mission: To find the precise names of the objects and properties which we will then use to write our VBScripts.

Once WBEMTEST has opened, there are two crucial points in mastering this tool.  The first point is to know what to put in the Connect, Namespace box.

  • Usually , I change the Namespace to root\cimv2 because I am mostly interested in Win32 objects like computers, disks, devices or event logs. (CIM means Common Information Model)
  • For Active Directory, I set the Namespace to root\directory\ldap
  • I leave the Namespace as root\default when I want the registry classes.

Here is the the second crucial configuration point.  Click on Enum Classes and select Recursive, now you will see hundreds of objects each with tens of properties.  I have to confess that when I am in a rush, I click on OK instead of Recursive and am very disappointed.  More haste – less speed.  So remember Recursive.

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

This week’s script

The idea behind this week’s script is to use WMI to interrogate the operating system and find out facts that are not easily displayed through a GUI, and certainly not all displayed in one box.

If the output of this script gives you just one idea for a script of you own then I will be happy.  My point, I am not saying that its not vital to collect ALL this data, however, one or two items in the output may spark an idea for a project.

‘ VBScript to interrogate the operating system
strComputer = "."
set objWMIService = GetObject("Winmgmts:\\" & strComputer)
set colOperatingSystems = objWMIService.InstancesOf("win32_OperatingSystem")
For each objOperatingSystem In colOperatingSystems
WScript.echo "Name : " & objOperatingSystem.name & vbCRLf & _
"CS Name : " & objOperatingSystem.csname & vbcrlf & _
"Caption : " & objOperatingSystem.caption & vbcrlf & _
"Version : " & objOperatingSystem.version & vbcrlf & _
"OS Type : " & objOperatingSystem.OsType & vbcrlf & _
"Install Date : " & objOperatingSystem.installdate & vbcrlf & _
"Free Phys Mem : " & objOperatingSystem.freephysicalmemory & vbcrlf & _
"Free Virt Mem : " & objOperatingSystem.freevirtualmemory & vbcrlf & _
"Current Users : " & objOperatingSystem.numberofusers & vbcrlf
Next

 Learning Points from the WMI Script

  1. strComputer = "."  I expect you remember that this means – local machine.  I challenge you to change the "." to the name of different computer on your network.
  2. set objWMIService   This is different from logon scripts which begin – Set WshShell  Here we connect to WMI rather than prepare a network shell connection.
  3. col means collection, as in colOperatingSystems (Note plural here)
  4. InstancesOf  Here is useful Method to case the win32_OperatingSystem object.
  5. WScript.echo  is an old friend to produce a message box.
  6. VBCRLF   This is useful control feature to produce a carriage return.
  7. Beware if you copy and paste to reorder the lines, the last line has no & _ where as the other items need that control sequence.
  8. I strongly recommend reviewing ezine 9.

Script to measure free disk space

Here is that script to measure your free disk space.

‘vbscript to measure the free disk space
On Error Resume Next
for each Disk in GetObject("winmgmts:").InstancesOf ("CIM_LogicalDisk")
WScript.Echo "Disk Partition : ",Disk.Path_.Relpath & vbCRLf & _
"Free Space : ", Disk.freespace
next

Challenge. 

USE WBEMTEST as a launch pad to write your own scripts, and send your best shot to me.   Its a strange phenomenon, but what I find is that if write a script that someone else is going use, I make it just that little bit better.  If you accept my challenge reply to this email attaching your script as .zip or txt file.

Troubleshooting Resources

WMI Tip  For much more on WBEMTEST, see my WMI Section here.

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

Ezine 110 WMI PowerShell  •Ezine 114 WMI Path  • Tool Kit