What better way to kick off a new year than with a cool new tool?
- By Chris Brooke
I’ve been writing a lot of WMI scripts lately. Indeed, for a while there
it seemed as if “Scripting for MCSEs” was becoming “WMI Scripting for
MCSEs.” There’s a reason for that. Quite simply: power. Pure, unadulterated
power! It’s odd that something named Windows Management Instrumentation
(a seemingly benign title) could pack such a wallop, but it does. Truth
be told, WMI makes it possible to easily perform tasks that were incredibly
difficult, if not impossible, via scripting alone. Consequently, it’s
important to have at least a working familiarity with WMI scripting—something
you can keep in the bottom of your scripting toolkit if the need arises.
And speaking of your scripting toolkit, the new tool I’m going to tell
you about this month will soon become as indispensable as Notepad and
XRay. It’s Scriptomatic.
Wizard for Making Initial Scripts
As much as I’d love to take credit for this little gem, I can’t. This
treasure comes from the brilliant minds of Microsoft’s “The Scripting
Guys.” Those of you who have heeded my advice in the past and visited
Microsoft’s scripting Web site (msdn.microsoft.com/scripting)
should already be familiar with these guys. They run the scripting clinic
. However, they also run the “Script Center” on Microsoft’s TechNet Web
site. This is where I found Scriptomatic. You can download it at www.microsoft.com/technet/scriptcenter/WMImatic.asp.
Scriptomatic is deployed as a hypertext application. What this means
is that it’s plain, old HTML with a file extension of .HTA. When you double-click
on this file to start Scriptomatic, it loads the WMI classes and puts
them into a nice drop-down list. Figure 1 shows the main screen of Scriptomatic
with the list box open. You select the class you want to work with, and
your script magically appears! Well, sort of. Like all Microsoft wizards,
it won’t do all the work for you. The scripts it creates are essentially
scriptlets that handle making the appropriate connection, retrieving the
associated properties, and echoing the data to the screen. Still, this
is quite impressive. You’re then free to pick and choose from the generated
code and place it into your script.
|Figure 1. The Scriptomatic can create a basic
script for any WMI class.
For coding additional script logic, all that’s required is for you to
substitute the relevant “WScript.Echo” lines with suitable code, such
as assigning the value to a variable or comparing it to another value
(or both). Figure 2 shows the Scriptomatic window with the code generated
from the Win32_Products class I’ve been using for the last few months.
|Figure 2. Scriptomatic’s code with the Win32_Products
The Really Cool Part
You have got to hand it to The Scripting Guys. Not only did they
create a tool that’s destined to become a staple in every self-respecting
scripter’s tool kit, but the manner in which they created it leaves room
for additional optimization. Because this is a hypertext application,
you can open it in notepad (or any HTML editor) and actually see how the
thing works! This provides valuable insight into hypertext applications
in general, as well as giving you the ability to modify exactly how the
scripts are created.
The subroutine that actually composes the script consists of only 23
lines of code. The “WScript.Echo” statements as well as the moniker of
the connection string are all hard-coded into the .HTA file. This can
easily be changed (as mentioned previously) to automatically assign the
value to a variable, perform a comparison, or any other number of operations.
The Really, Really Cool Part
Finally, because a hypertext application is essentially a script that
runs in an Internet Explorer window, you could (if you so desired) take
the code from the .HTA file, modify it to suit your needs and save it
as a regular script. The only thing you really give up is the basic interface
and the drop-down list. Simply pass the selected class as an argument
and configure it to save the script automatically to your scripts directory.
At its heart, the Scriptomatic is a powerful script that allows you to
determine what WMI classes are available, then enumerate all the properties
and methods for each class. How cool is that?
Scriptomatic should prove invaluable as you get more familiar with
WMI scripting. (Although I still maintain that those who wish to fully
understand WMI scripting pick up a suitable reference text.) And, of course,
you’ll need to refer to this column monthly (whether or not the topic
is WMI) just to be on the safe side (please forgive my shameless plug).
Nevertheless, Scriptomatic will surely provide immeasurable value to you
as you move forward. Just don’t forget: You saw it here first.
Chris Brooke, MCSE, is a contributing editor for Redmond magazine and director of enterprise technology for ComponentSource. He specializes in development, integration services and network/Internet administration. Send questions or your favorite scripts to [email protected].