Windows Script Host (ASP)Windows Script Host (WSH) is a scripting host for ActiveX Scripting Engines such as PerlScript. As a host, WSH enables you to use the scripting language from the command-line and from within the Windows desktop with the WSH features.
Running a WSH FileIn order to execute a WSH-file, use one of two executable files depending on your needs: WScript.exe for Windows desktop files and CScript.exe is for command-line scripts. You can set the behavior and appearance of these executed scripts by running the executable without providing a file; if so, WScript will display a properties page that you can modify, and CScript will show the available switches. At work, WSH enables you to use more than one scripting engine in the same file, include typelibraries, and run more than a single job from one file to name a few.
The Object ModelImplemented as an object-model, WSH provides a simple interface for its tasks. As you author, the WSH file uses XML as its markup for separating the elements. Let's look at a simple WSH file that prints "Hello World!".
The XML Job-elements encloses the ID of the Job that is run, and the script elements define PerlScript as the script language to use. You will experience different results depending if you execute this from the command-line or from the windows desktop. The first instance will print text to the screen, but the Windows desktop will pop up a messagebox with "Hello World!" Next, let's look at what the WScript object has to offer.
The Windows Script ObjectThe WScript object is a built-in object; therefore, you do not need to create a specific instance of it within your WSH. On the contrary, the object in place is used to create instances of most other objects exposed through the WSH programming interface.
In addition to the above, you can specify a second parameter in the call to
For the same result as above, you can use the
Retrieves an Automation object from a file or an object specified by the strProgID parameter.
Quit and process and optionally provide an integer which represents an error code.
Places the script process into an inactive state for the number of milliseconds specified and then continues execution.
And its properties are:
Provides the IDispatch interface on the WScript object
Returns a pointer to the WshArguments collection or identifies arguments for the shortcut to the collection.
Returns a string containing the full path to the host executable file or shortcut object.
Returns a string containing the friendly name of the WScript object.
Provides a string containing the name of the directory where WScript.exe or CScript.exe resides.
Provides the full path to the script currently being run.
Provides the file name of the script currently being run.
Exposes the write-only error output stream for the current script. Only applicable with CScript command-line WSH files.
Exposes the read-only input stream for the current script. CScript only.
Exposes the write-only output stream for the current script. CScript only.
Returns the version of Microsoft Windows Script Host.
On a final note, if you are using Cscript.exe and passing arguments to the file, you can read the arguments as follows:
The WShShell ObjectThe WshShell object must be instantiated by the WScript object.
An interesting method of the WshShell object is the ability to activate an application window and putting it in focus. This is done by calling AppActivate either with the title in the title bar of the running application window as a parameter or by using the task ID as a parameter.
The WindowStyle can be an integer between 0 through 10, and WaitOnReturn is a boolean value or 1 (TRUE) or 0 (FALSE). FALSE is the default value it means that an immeditate return to script execution contrary to waiting for the process to end is preferable. It also returns an error code of zero while TRUE returns any error code generated by the active application.
Creating ShortcutsIn addition, you can create shortcuts. Either you create a dekstop shortcut or a URL shortcul. The method call
With the WshShortcut-object, one method exists, so it is mainly properties regarding the shortcut that you need to set. The
Special FoldersThe WshShell object can also return a WshSpecialFolders object which contains paths to shell folders such as the desktop, start menu, and personal documents.
Working With the RegistryThe WshShell object provides functionality for working with the registry. The three methods for this are:
RegWrite requires a few extra parameters:
The name is a fully qualified string such as HKCU\ScriptEngine\Val where the same rules apply for key and value as previously mentioned. The Type-parameter is optional, but if used, it must be one of the following data types:
MiscellanousExpands the requested environment variable from the running process:
In addition, log an event in the NT event log or WSH.log (Windows 9x) file using:
Target is the name of the system on NT, thus only applicable on NT. The Type of event is either
This method returns a boolean value indicating success or failure. Another method is Popup, which sends a Windows messagebox up on the screen.
The method allows you to define the text to pop up, alternatively seconds to wait before closing window, the title of the window, and lastly the type of buttons available in the window. They can be:
The value that you choose can also be combined with an icon:
The return values returned to
The WshNetwork ObjectThe WshNetwork object exposes some network functionality. To begin:
User and password are parameters two parameters with given meaning. Localname and Remotename are the names of the printer resource. Set UpdateProfile to TRUE for storing this mapping in the user profile. Next, AddWindowsPrinterConnection() adds a printer just as you would add one using the control panel. On Windows NT/2000 the only parameter you need to call this method with is the path to the printer while windows 9x requires you to specify the driver to use, and optionally specify which port to which it is connected. In the last event, the syntax is:
As easily as adding a printer, you can remove a printer. Simply do
If you set $Force to TRUE (1), it will remove the connection regardless if it is being used, and setting $UpdateProfile to true will remove any user profile mapping.
If you're happy with your printers, you can set one of the printer as your default printer by a quick call:
To return a collection of all your printers, call:
Then use the Count-property to retrieve the number of items in the $Printers collection object.
When you want to map a drive to a network share, you can use the MapNetworkDrive method.
Remove a network drive using the now familiar syntax
or enumerate the network drives as:
The three properties of the network object are ComputerName, UserName, and UserDomain.
XML Element ReferenceLike Windows Script Components, the Windows Script Host has a set of XML elements that can be deployed. For a basic understanding of how they are used, please refer to the section about Windows Script Components.
The Job ElementThe Job element is used to define the beginning and the end of the components. It encapsulates all other tags. Should your WSH file contain more than one job, encapsulate them within a <package> element. When declaring jobs, the ID attribute is optional.
You can also set a boolean value of true (1) or false (0) for error checking or debugging by using the additional tag
The Script ElementThe script element lets you define the scripting language to use, and then with its closing-tag functions as delimiters for the script code.
The Resource ElementThe resource element is a placeholder for strings or numeric data that should be separate from the script commands yet may be used within the script.
You use the
The Reference ElementYou can import external type libraries by using the reference element. By importing a type library, you will be able to naturally access the constants that belongs to it, too.
AUTHOR AND COPYRIGHT
Written document copyright (c) 2000 Tobias Martinsson. All rights reserved.
When included as part of the Standard Version of Perl, or as part of its complete documentation whether printed or otherwise, this work may be distributed only under the terms of Perl's Artistic License. Any distribution of this file or derivatives thereof outside of that package require that special arrangements be made with copyright holder.
Irrespective of its distribution, all code examples in this file are hereby placed into the public domain. You are permitted and encouraged to use this code in your own programs for fun or for profit as you see fit. A simple comment in the code giving credit would be courteous but is not required.
Windows Script Host is copyright (c) 1991-2000 Microsoft Corporation. All Rights Reserved.