Skip navigation

Convert WSH Scripts into Console Tools

Downloads
41501.zip

\[Editor's Note: Share your scripting discoveries, comments, problems, solutions, and experiences with products. Email your contributions (500 words or less) to [email protected]. We edit submissions for style, grammar, and length. If we print your submission, you'll get $100.\]

Pipelining, or redirecting the output stream of an application to the input stream of another application, is a valuable capability of console applications (i.e., executable programs or scripts). You can pipe the output stream from a console application to any console application that can process an input stream and thus generate complex solutions with no extra scripting.

As the Windows Script Host (WSH) Help (http://msdn.microsoft.com/library/en-us/script56/html/wsprostdin.asp) documents, WSH can use WScript.StdIn to read from the standard input stream (stdin). Although the WSH Help file mentions that you can use only CScript and not WScript when using the StdIn property, it doesn't mention one problem. Even if you set WSH to use CScript as the default scripting host, WSH breaks the connection to previous commands when it looks up the default host. If you use command output as script input, WSH returns the error (null): The handle is invalid.

To solve this problem, you can specify CScript as the scripting host on the command line. To do so, enter

cscript c:\scripts\myscript.wsf

However, this method introduces another problem: You must specify the full path to the WSH script.

You can use a short command-shell script to solve all these problems. I use the csh.cmd script, which contains just one line:

@cscript "%~dp$PATH:1"%*

To launch WSH scripts, I specify csh with the WSH script name, including its extension, then follow this command with any arguments my script uses.

To help me demonstrate how csh.cmd works, I wrote the uptime.wsf script. Listing 1 shows an abbreviated version of the script. (To download the complete script, go to http://www.winnetmag.com/windowsscripting and enter 41501 in the InstantDoc ID text box.) Uptime.wsf returns the time since last boot for one or more computers; if you supply the / argument, the script tries to read a list of computers from stdin and checks the uptime for each computer. On my system, the uptime.wsf script is in the C:\bin\scripts folder, which is in my search path.

Suppose I want to know the time since last boot for a list of PCs in my domain. I keep this list in the C:\data\computers.txt text file. To force the uptime.wsf script to run properly, I'd use csh to launch the script:

csh uptime.wsf / < C:\data\computers.txt

In csh.cmd, the code "%~dp$PATH:1" retrieves the first argument (i.e., uptime.wsf), which is the file that csh.cmd will search for. When csh.cmd finds uptime.wsf, the $PATH environment variable expands to the containing folder (i.e., C:\bin\scripts). The %* parameter expands to uptime.wsf cmd /a, making the line literally

@cscript "C:\bin\scripts\"uptime.wsf / < C:\data\computers.txt

If you have a directory path that contains spaces, you must embed the path in quotation marks. To handle WSH scripts whose names contains spaces, you must also embed the WSH script's name in quotes.

Redirection or pipelining is as complex as it is powerful. For a more thorough discussion of the technique, see the Redirection Operators and Filtering sections in Windows Server 2003, Windows XP, or Windows 2000's Help.

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish