Windows Programming/Windows Script Host< Windows Programming
The DOS programming model, with it's clumsy batch scripting capabilities was quickly wearing out, and Microsoft needed to add an alternative scripting environment to Windows. The result of this was the Windows Script Host (WSH). WSH comes in two flavors: a console mode version (csh.exe or cscript.exe) and a non-console version (wsh.exe or wscript.exe).
WSH actually isn't a new language, but is instead an environment for programming in other Active Scripting languages. Active Scripting languages are programming languages that are implemented as COM components. Any Active Scripting language installed on your computer can be used with WSH. By default, WSH can be programmed using JScript and VBScript, but other Active Scripting languages can be installed and run in WSH. One of the more popular 3rd party Active Scripting languages is PerlScript, which is often installed as part of the ActivePerl distribution.
WSH scripts also include a certain amount of XML code, that helps to control the environment of the running script. The language being used to script must be specified in an XML tag.
WSH is an Object Oriented environment, and WSH provided a large number of objects that the Active Scripting languages may tap into.
Another Microsoft technology, Active Server Pages (ASP) is a similar program to WSH in that it is a language-neutral scripting environment that uses installed Active Scripting languages. However, ASP is used as a server-side scripting tool for generating webpages. WSH, in comparison is more well suited for use in scripting a local environment (although WSH can be used as a server-side website scripting engine as well, if you dare!).
Writing a WSH ScriptEdit
A WSH script file is a plain-text file, that may use a generic ".wsh" file extension. Also, many other file extensions are associated with WSH, and may be more descriptive then the simple .wsh extension. For instance, a WSH script that is primarily VBScript may have the extension ".vbs", and a WSH script that is JScript may have the extension ".js".
Each WSH file must have an XML "JOB" object. Each script may have multiple Jobs, but each job must have an unique ID. For instance, here are some sample job tags:
Next, we must specify the scripting language that we will be using to implement a specific job:
<script language="JScript" src="IncludeFile.js"/>
Note from the example that we can include an external source file into our script file, to be run. In a Job object, all the instructions are executed, from the top to the bottom. In each job, we may have multiple different scripting languages in use, in individual blocks.
At the end of a script section, we must close the tag with a "</script>" tag. At the end of a job, we close with a "</job>" tag.
WSH provides the WScript object, that has a number of methods and fields for use in a WSH script. The WScript object never needs to be instantiated, and it is always available in any script. As an example, the "Echo" method of the WScript object prints out a certain string. The csh.exe (Console Version) of WSH will print the string to the console, while the wsh.exe (Non-Console Version) will print the string to a message box. Here is a multi-lingual example:
<job id="TestEcho"> <script language="Jscript"> WScript.Echo("Printing from JScript"); </script> <script language="VBScript"> WScript.Echo "Printing from VBScript" </script> <script language="PerlScript"> $WScript->Echo("Printing from PerlScript"); </script> </job>
Note, of course, that you can't use the PerlScript if you haven't installed it on your computer.
There are a number of other XML tags that can be used in a WSH script.
The <description> tag contains a certain amount of plain text that describes the current file. For instance:
<description> This script writes a status entry to the log file, and updates the website. </description>
Now, this may seem like a difficult way to just write a comment, but the <description> tag also has another purpose. Let's say that we save the above code snippet into a script file, such as "testdesc.wsh". Now, if we run our script with the "/?" argument, we get the following result:
C:\>testdesc.wsh /? This script writes a status entry to the log file, and updates the website.
When we send the argument "/?" to our script, all the text that is written in the <description> tag is printed to the console.