Downloads
26069.zip

Do you have a scripting-related question or problem? You can send your question or problem to winscriptsol@winnetmag.com.

I work for a large company that, depending on the task, uses Perl or Windows Script Host (WSH) for scripting. I often use Perl's Format command to define and create text-based report templates that contain column headings and widths and for other formatting tasks. But I can't figure out how to format output in VBScript. I know that VBScript's basic formatting capabilities use string constants (e.g., vbTab, vbCrLf), but I need a more powerful tool. Do WSH and VBScript have capabilities similar to Perl's Format command?

Yes and no. WSH and VBScript don't provide built-in text formatting capabilities like Perl does. However, the ShowResults method in Windows XP might meet your needs. ShowResults, part of a new command library called cmdlib.wsc, supports many of XP's new command-line scripts. You'll find cmdlib.wsc in the \%systemroot%\system32 directory.

As its file extension implies, cmdlib.wsc is implemented as a Windows Script Component. WSCs are COM components written as script and are a method for packaging a common set of routines you want to use in multiple scripts. You can use the methods and properties cmdlib.wsc provides the way you would any other COM component that's installed and registered on your computer. Furthermore, because WSCs are written in script, you can open cmdlib.wsc in your favorite text editor to examine its contents and implementation.

The command library's ShowResults method requires four steps:

  1. Create a reference to the component by using VBScript's CreateObject function in combination with the component's programmatic identifier (ProgID), which in this case is Microsoft.CmdLib.
  2. Set the command library's ScriptingHost property to the name of the script host, which you can easily do by using the WScript Application property.
  3. Create and initialize the variables that tell ShowResults how to format the output.
  4. Call ShowResults.

The sample script in Listing 1, page 6, uses Windows Management Instrumentation (WMI) to retrieve the DisplayName, StartMode, and State properties from the computer's services, then formats the output by using the command library's ShowResults method. The script begins by initializing the variable strComputer with the target computer's name. In WMI, the dot (".") represents the local computer.

At callout A in Listing 1, the script performs Step 1 by creating a reference to the command library component. Next, the script sets the command library's ScriptingHost property to the value WScript.Application. This step is mandatory; you must set the command library's ScriptingHost property to WScript.Application before you can use any of the 16 methods that the command library component provides.

At callout B, I define and initialize the variables that tell ShowResults how to display the data. ShowResults accepts the six input parameters that Table 1, page 6, defines. Now that you know a little about ShowResults' six parameters, you can make the following observations about the table by examining the values of the variables defined at callout B:

  1. ShowResults creates a three-column table.
  2. The three column headings are Display Name, Start Mode, and State.
  3. The first column is 32 characters wide; the second and third columns are 12 characters wide. Note that text exceeding the column width is truncated.
  4. The column headings are displayed because blnDisplayLabels is set to True. All three columns are shown based on the three False values assigned to the arrHideColumn array.

Next, the script uses WMI, the WMI Scripting Library's ExecQuery method, and the WMI Query Language (WQL) to retrieve the values of the DisplayName, StartMode, and State properties for all the services installed on the target computer. The query returns a collection, which is stored in the variable colServices.

The code at callout C is the trickiest part of the script. To satisfy ShowResults' data requirements, you must package the data that will be displayed as an array of records, where each record is yet another array. To visualize this setup, think of a simple spreadsheet or table in which each row (service) contains three columns (DisplayName, StartMode, and State).

Using VBScript's ReDim statement, I set the size of the arrRecords array equal to the number of items that the WMI ExecQuery method returned. Next, I loop through each item in the colServices collection. During each iteration of the For...Each loop, I create a record array that contains the three properties and store the record in the arrRecords array. At the conclusion of the For...Each loop, I'm ready to call ShowResults, which occurs at callout D. Figure 1 shows a portion of ShowResults' output. Notice how the second record's Display Name value is truncated because it exceeded 32 characters.

The command library component provides 15 other methods: checkScript, vbPrintf, getHostName, getUserName, getDomainName, LengthinBytes, getPassword, trapError, getArguments, wmiConnect, packString, getMaxStringLen, validateDateTime, changeToWMIDateTime, and matchPattern. You can learn more about these 15 methods by examining cmdlib.wsc in Notepad.