Contents of Guy's Scripting Ezine No 10 - WMI Part 2Note, See more on WMI a whole section here
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 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 download WBEMTEST here.
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.
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
- 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.
- set objWMIService This is different from logon scripts which
begin - Set WshShell Here we connect to WMI rather than prepare a
network shell connection.
- col means collection, as in colOperatingSystems (Note plural here)
- InstancesOf Here is useful Method to case the win32_OperatingSystem
object.
- WScript.echo is an old friend to produce a message box.
- VBCRLF This is useful control feature to produce a carriage
return.
- Beware if you copy and paste to reorder the lines, the last line has no &
_ where as the other items need that control sequence.
- I strongly recommend reviewing ezine 9.
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.
For much more on WBEMTEST, see my WMI
Section here.
Their topics and material are ideal for getting you started with VBScript. The
videos are easy to follow and you can control the pace. Try their free demo material and then see if you want to buy the full package.
See more about VB Script Training CD.
|