Write the Perfect Script
Need to go deep and seize control of your Windows systems? One of these tools can help you perfect the art of scripting.
- By Bill Boswell
At some point in your career, you'll grow impatient with fancy graphic interfaces and yearn to take more control of your systems. You've heard how scripts can unlock your computer's secrets. So you open Notepad and type—Wscript.Echo "Hello World!" Double-click the file in Explorer, and you're rewarded with the popup in Figure 1
. Suddenly, you're a scripter.
Before you know it, you're scouring newsgroups for sample scripts and searching for help on syntax and functions. You're subscribing to RSS feeds for every scripting Web site and blog you can find. Most importantly, you realize Notepad is a pathetic tool for doing serious scripts.
If you tend to keep your scripts short and you don't want to spend much money, you might be satisfied with a simple script editor like Notepad (see sidebar, "Simple Script Editors"). To be truly productive writing large or complex scripts, though, you'll need an editor that provides a complete scripting environment. You'll pay a few extra bucks, but the savings in time and frustration and the level of control you'll have over your Windows systems make this an excellent investment. An integrated scripting environment provides an array of features designed to simplify the development and testing administrative scripts.
|Figure 1. Just by typing a simple message into Notepad, you've entered the world of scripting.
For this roundup, we considered four of the leading scripting environments:
- AdminScriptEditor 2.2
- OnScript 1.1
- PrimalScript 4.0
- VbsEdit/JsEdit 2.0
The scripting environments included in this roundup all support one or both of the native Windows script interpreters—VBScript and JScript. This means we did not include tools like Komodo from ActiveState. If you write scripts using Perl, Python, PHP, Ruby or Tcl, you might want to consider Komodo as well.
|Click image to view larger version.
A script editor's features generally fall into four discrete categories—editing aids, coding aids, debugging aids and distribution aids. When it comes to comparing usability, a simple list of features can't tell the whole story, though. Here are a few examples of how these four compare:
- All of the editors let you comment on blocks of text with a toolbar button, but only AdminScriptEditor (ASE) puts this feature in a properties menu.
- AdminScriptEditor and PrimalScript let you use "code folding," a technique for collapsing regions of code so you can focus on one section at a time. OnScript takes a different approach by offering an Outliner that displays the names of functions and subroutines, but not arbitrary regions of code.
- AdminScriptEditor and PrimalScript Pro Professional edition have wizards to simplify creating Active Directory Services Interface (ADSI) code.
- PrimalScript Pro has a database browser to generate code for accessing a database and AdminScriptEditor has a feature for generating Access and SQL Server database connection code.
- All the editors let you edit multiple files at once, but only AdminScriptEditor, PrimalScript and OnScript support split-screen editing of the same file. PrimalScript and OnScript support a single split and ASE supports a four-way split.
- Code re-usability is a great convenience. OnScript, AdminScriptEditor and PrimalScript let you browse through a library of code snippets and inject them into the current script. ASE provides extensible wizards for injecting code to access Registry keys, Active Directory objects and so on. PrimalScript Professional can compare two files and show you the differences, a nifty way to find glitches in similar scripts.
- If you share responsibility for updating scripts, you'll like the way PrimalScript integrates into SourceSafe, Microsoft's version control developer utility, so you can enforce version control.
- AdminScriptEditor can save scripts in HTML format so you can post the code on a Web site while retaining syntax coloring. ASE also lets you constrain the editor window to a given number of columns, which helps avoid word wraps when cutting and pasting lines.
- PrimalScript can record, play and store macros, as well as assign macros to specific keys.
The list of small but significant differences goes on and on. Keep in mind that this is a highly competitive market. If a product lacks a particular feature, you can bet it will be included in the next version. Here's a more detailed look at some of the functional categories you'll find in all four of the scripting environments reviewed here.
|Simple Script Editors
Even serious scripters should pay attention to these simple script editors. They provide a quick and easy environment for making changes.
This freeware Notepad replacement is small, fast, highly useful and comes with its own source code if you want to do any customization. The product is free, but donations to Amnesty International are requested.
GVIM (Graphical VI iMproved)
Even if you've never used VI, you'll appreciate this tool's amazing range of features and speed. Although the GVIM menus expose a wide variety of features, the true power comes from a command-line interface that accepts a dizzying array of commands. You can extend those with a phalanx of add-ons contributed by a fanatical band of “vimmers.” GVIM is free, but if you want to support development, a sponsorship costs 10 Euros. Donations to needy children in Uganda are also requested.
If you want lots of features but prefer the functionality of a GUI-based editor, you'll like Crimson Editor. Crimson Editor sports a handsome and useful editing window, extensive language support, a tabbed interface for editing multiple files, fast and simple macro recording, a fantastic array of text display tools and hotkey support for every feature. Crimson Editor is freeware with a suggested sponsorship donation of $10 a year for personal use and $30 a year for professional use.
As you write longer and more complex scripts, it will be harder to troubleshoot errors. At some point, you'll want a debugger to help you figure out why a particular script won't run correctly.
AdminScriptEditor and OnScript use Microsoft's Script Debugger, which lets you set breakpoints and step through the code one line at a time. OnScript opens the debugger, then jumps right to the offending line that's causing the error.
PrimalScript and VbsEdit have integrated debuggers (see Figure 2) that make troubleshooting a more seamless experience. Both editors display the values assigned to variables during debugging, but only PrimalScript lets you view the variable values while you step through the code.
|Figure 2. PrimalScript's integrated debugger streamlines the process. VbsEdit also has an integrated debugging tool. (Click image to view larger version.)
As the names imply, Adersoft's VbsEdit and JsEdit are limited to editing files in their respective languages. The remaining editors support a variety of file types, including XML, CSS, INI and batch files.
PrimalScript has the broadest language support. In addition to the languages mentioned above, it supports Macromedia Flash ActionScript, an array of Web files and many programming languages and frameworks like .NET.
OnScript supports HTA files, which is a common way to put a graphical interface on a script. It also supports Perlscript and Pythonscript.
AdminScriptEditor focuses on VB-Script, KiXtart and AutoIT, but plans to support HTML and ASP files (including HTA) in its next major release.
KixTart is a scripting language developed by Ruud van Velsen that has wide community support. (In fact, AdminScript-Editor began life as a KiXtart editor.) AutoIT is a specialized language designed to simplify GUI-based programming for large numbers of machines.
All of these scripting environments have online help, except for VbsEdit. Where you'll really need the help is figuring out how to use the functions in a particular scripting language, how to use the properties and methods in automation objects accessed by a script, and how to wring the most functionality out of major scripting interfaces like ADSI and Windows Management Instrumentation (WMI). This is where you really get your money's worth from an integrated scripting environment.
For example, all of the editors have integrated help for VBScript. PrimalScript lets you search an "Information Nexus" for help on a particular function. OnScript and VbsEdit have the Microsoft VBScript 5.6 help file integrated into their editor, so all you need to do is press F1 to open the Help file. AdminScriptEditor lets you right-click a VBScript function and choose a help feature, which opens a window displaying the syntax and giving examples (see Figure 3).
|Figure 3. You can get to special help for VBScript functions from the property menu in AdminScriptEditor. (Click image to view larger version.)
Each of the editors also has language-sensitive, popup-syntax help (see Figure 4). This helps remind you of a function's required elements. OnScript doesn't fully expand the syntax of a selected class, which limits the usefulness of its popup help. VbsEdit's help items provide the most information, including not only the syntax, but the purpose of the object class as well.
|Figure 4. The popup syntax help in VbsEdit. A similar feature is available in all the scripting environments included in this roundup. (Click image to view larger version.)
COM Code Completion
There's one feature that's even more useful than syntax help—COM code completion. This is patterned after a Microsoft trademarked term called IntelliSense, and comes into play when you create a COM object, then use it later in the script. The editor displays a menu of available methods and properties for that particular COM object and lets you complete the code with a selection from the menu.
Because IntelliSense is trademarked, AdminScriptEditor calls this feature ScriptSense. PrimalScript calls it PrimalSense, and OnScript simply calls it COM code completion. VbsEdit has no documentation and therefore doesn't need to concern itself with trademark issues. Not all the COM code completion features are equal, though. Consider the following lines of VBScript code:
Dim objExcel, objWorkbook, objSheet1, objSheet2
Set objExcel = CreateObject('Excel.Application")
Set objWorkbook = objExcel.Workbooks.Add()
Set objSheet1 = objWorkbook.
When you type the period in the final line, you'd want to see a menu of available methods and properties for the Workbooks class. Only VbsEdit and PrimalScript actually provide a menu. The other scripting tools should have this feature in their next release.
Object Type Library Browsing
The real secret of power scripting is making good use of the vast array of automation objects in Windows. You can search the Platform Software Developer's Kit (SDK) to learn about these, but often the documentation doesn't include truly useful samples. And, of course, you won't learn about third-party automation objects in the Platform SDK.
Three of the editors (the odd guy out is VbsEdit) have a feature that lets you browse the locally stored registered automation objects and select properties and methods from a tree. For example, let's say you're using ADSI in a script to query Active Directory and display a user's last logoff time. You know that ADSI uses the Active DS Type Library and that user attributes are stored in an object class called IadsUser. You can use a type library browser to view the properties of the IadsUser class and find the spelling and syntax for the LastLogoff property (see Figure 5).
|Figure 5. The Object Type Library browsers in PrimalScript (left) and OnScript (right). (Click image to view larger version.)
If you look closely, you'll see that although both editors have a tabbed window to get at the type library browser, PrimalScript is a bit harder to use because it doesn't sort the classes alphabetically and includes many classes that aren't useful for scripting. OnScript has a nifty feature that lets you insert sample code from a selected object's property listing.
|Figure 6. AdminScriptEditor's Type Library Browser has a Search feature that helps you look up properties by name or partial name. (Click image to view larger version.)
AdminScriptEditor simplifies the process even more. You can load a selected class in the viewer, then use a Search feature to find a particular property or method (see Figure 6). This search feature makes it easier to figure out which properties of a selected class might be useful in a given script.
You can't really take advantage of the power of scripting in a distributed environment until you learn how to access WMI from within your scripts. WMI has a well-deserved reputation for being difficult to master.
The Scripting Guys at Microsoft provide a ScriptoMatic utility. This HTA utility lets you select a WMI class and inject code to display the properties of the class for all instances on a target machine.
All of these editors have a similar feature. OnScript straps ScriptoMatic onto its interface and lets you insert the output code into an open file.
PrimalScript has a menu item for exploring WMI classes and injecting enumeration code. VbsScript does something similar with a toolbar menu.
|Figure 7. AdminScriptEditor's WMI wizard shows the property selection option. (Click image to view larger version.)
AdminScriptEditor takes WMI support to the next level. You can not only select a WMI class, you can select individual properties from within that class (see Figure 7). You can also set conditionals to limit what instances of the class will be included in the output, such as specifying a minimum freespace when listing disks. Here is a sample script inserted by the wizard:
Set wmiColl = GetObject("WinMgmts:root/cimv2")._
ExecQuery("Select * FROM Win32_DiskDrive " & _
"WHERE Size > 60000000000")
For Each wmiObj In wmiColl
The list of features for PrimalScript, OnScript, AdminScriptEditor and VbsEdit/JsEdit are quite similar, plus this is an extremely competitive market. So when you see a new feature in one, you can expect to see something similar in the others before long.
AdminScriptEditor has a certain elegance about how it presents its features and options, from the simplified library browsing and WMI scripting to its context-sensitive help with examples. PrimalScript is right up there as well, with a thoughtfully conceived help search, extensive language support and an integrated debugging tool.
OnScript has some handy troubleshooting and help features, including a slick outlining tool for examining scripts in process. It also integrates with Microsoft's debugging tool and brings you right to the troublesome line for correction. VbsEdit features a helpful integrated debugging tool. Both OnScript and VbsEdit integrate with the Microsoft VBScript help file as well. VbsEdit and JsEdit are limited to working in their respective languages, however, and lack context-sensitive help.
Each of the integrated scripting environments covered here have trial downloads with generous evaluation periods. Try each one for a while to see which best fits your needs before evaluating solely on budget or list of features. That's the best and most accurate way to make your evaluation. All of these are stable, solid products that deliver considerable value.
- A complete comparison of features can be downloaded here (Excel format)
- For a free download of AutoIT, go to: www.hiddensoft.com/autoit
- Get more information and download the most current version of the KiXtart scripting tool at: www.kixtart.org
- To learn more about the Komodo scripting tool from ActiveState, go to: www.activestate.com
- Download the Microsoft Script Debugger here
- ScrptoMatic can be found here
- Download a copy of Notepad2 here
- Download a copy of GVIM here
- Download a copy of Crimson Editor here