Windows Script Host






Windows Script Host

Microsoft's Windows Script Host (WSH) is a language-independent scripting host for 32-bit Windows operating systems. It provides the most powerful functionality of all the scripting methods discussed so far. Windows Scripting Host works seamlessly with all scriptable objects available to Windows, allowing you to create complex, scripted applications. By providing extensive scripting capabilities combined with support for multiple scripting languages, WSH is quickly becoming the scripting method of choice.

Note 

By default, Windows Script Host supports two languages: VBScript and JScript. All the Windows Script Host examples in this book are written in VBScript.

CSCRIPT and WSCRIPT

Windows Script Host is controlled by two executables, CSCRIPT and WSCRIPT. CSCRIPT is the command-line host utility that is commonly used to run tasks in the background or in a command prompt. WSCRIPT is the graphical host utility commonly used to interact with the user. These two executables support many command-line parameters, as shown in Figure.

Figure: Windows Script Host parameters.

Parameter

Description

//B

Disables command prompt user input.

//D

Enables active debugging.

//E:engine

Uses the specified engine at script execution.

//H:CSCRIPT

Sets CSCRIPT as the default execution host.

//H:WSCRIPT

Sets WSCRIPT as the default execution host.

//I

By default, enables command prompt user input.

//JOB

Executes a WSC job.

//LOGO

By default, displays logo at script execution.

//NOLOGO

Suppresses logo at script execution.

//U

For CSCRIPT only, specifies to use UNICODE for I/O operations.

//S

Saves options on a per user basis.

//T:seconds

Specifies the maximum time, in seconds, a script is allowed to run.

//X

Executes the current script within the debugger.

//?

Displays help context.

What in the World Is an API?

Before you can start scripting with Windows Script Host, you should have a basic understanding of Application Programming Interfaces (APIs). An (API) is a collection of functions that the operating system or application can call on to perform many different tasks. By using a common set of code, applications can perform operations identical to those that the operating system performs. These APIs are normally stored in DLL files. Although programmers can access DLLs through compiled applications, scripters need to find another method of access.

Working with COM Objects

An object is simply a collection of functions that perform similar tasks. COM (Component Object Model) objects expose API methods and properties, providing a way for scripters to access APIs in their scripts. These objects are normally stored in OCX (OLE custom control) or DLL files. To gain access to a COM object, you use the CreateObject function to load an object into memory, connect to the object, and set this connection to a variable. This is called instantiating an object and is performed as follows:

Set variable = CreateObject("object")

Once the instance is created, you can use this variable throughout your script to access all the methods within the object.

The Windows Script Host object model (see Figure), is a hierarchal, organized collection of objects, mostly stored in a file called WSHOM.OCX located in the Windows\System or Winnt\System32 directories. Each of the core objects contains its own methods and properties to perform specific tasks.

Click To expand
Figure: The Windows Script Host object model.

The Wscript Object

The Wscript object is the core scripting object. It allows you to collect information about your script, work with arguments, and call other ActiveX objects. The Wscript object contains the methods to instantiate other objects and is automatically instantiated every time a script is run. The most commonly used Wscript method is the Echo method, which sends output to the screen:

Wscript.Echo "Some Output"

The WshNetwork Object

The WshNetwork object provides access to Windows network functions. You can use this object to work with network connections and perform various network-related tasks. The most common tasks used with this function are mapping printers and drives, and obtaining a computer's network information.

The WshShell Object

The WshShell object provides direct access to Windows and registry functions. You can use this object to work with shortcuts, display messages to users, manipulate the registry and environment variables, and run external commands.

The FileSystemObject Object

Is there an echo in here? Although not actually a part of the Windows Script Object model, the FileSystemObject object, contained in SCRRUN.DLL, can be used to access and manipulate the file system. Through this object, you can perform almost any file management task that you perform manually.

Now that you are familiar with the Windows Script Host Object model, you can start using subroutines to organize your scripts.

Subroutines

Throughout this book, you will find various subroutines reused in examples. Subroutines allow you to take a section of repeated code and make it accessible by simply calling it. Subroutines accept multiple parameters, allowing you to pass arguments to the subroutine for manipulation. Windows Script Host provides two types of subroutines: sub procedures and functions.

Sub Procedures

A sub procedure performs a series of actions without returning any data. A typical use of a sub procedure is to perform file manipulation, working with text files, or to display user prompts. A sub procedure is structured as follows:

Sub SubName (arguments)
  Code
End Sub

Here, SubName is the name given to the sub procedure; arguments are the parameters passed to the sub procedure (separated by commas); and code is the script action(s) to perform.

Note 

Any variables used within a sub procedure will not be accessible outside of the sub procedure, unless they are explicitly declared beforehand.

Functions

Similar to KiXtart, a function is a procedure used to return data. A Windows Script Host function is structured as follows:

Function FunctionName (arguments)
  Code
End Function

Here, FunctionName is the name given to the function; arguments are the parameters passed to the function (separated by commas); and Code is the script action(s) to perform. To return a value outside of the function, you should name a variable from within your function with the same name as your function and set a value to it.

Note 

While the KiXtart closing statement for a function is "EndFunction", Windows Script Host requires a space between the words "End" and "Function."

Windows Script Host Example

You now probably realize why I've saved the most difficult scripting example for last. As you'll see, using Windows Script Host requires a bit more set up work but the work is worth it because of all of the flexibility and power that you gain. Let's revisit the task of displaying the name of your local computer with the help of a WSH script.

Displaying the Computer Name

To display the name of the local computer using Windows Script Host, proceed as follows:

  1. Create a new directory to store all files included in this example.

  2. Download and install the latest version of Windows Script Host, from http://www.microsoft.com, to the new directory.

  3. Again, use your favorite text editor to create a new script file. The following instructions should be placed in the file:

         Set WshNetwork = CreateObject("WScript.Network")
         WScript.Echo WshNetwork.ComputerName
    

The first line uses the CreateObject method to create an instance of the built-in Wscript.Network object and stores the instance in a variable called "WshNetwork." The last line accesses the Wscript.Network's ComputerName property and displays it with the Wscript.Echo method.

To finish your script file, simply save it using a filename that you will remember.

  1. Select Start|Run and enter "cscript scriptfile.vbs."

Recall that CSCRIPT is the name of the command-line host utility. Here, scriptfile should be the full path and file name where your file is stored (Step 3).

Limitations of Windows Script Host

Windows Script Host's built-in graphical support is extremely limited. Although it does offer popup capability, WSH does not include the custom screen manipulation capabilities that KiXtart has to offer. Finally, as the name implies, Windows Script Host can only be used under Windows.

When to Use Windows Script Host

Windows Script Host can be used to manipulate windows, work with files, modify the registry, and more. By itself, Windows Script Host is best suited for background tasks with little or no interface. When combined with COM automation, WMI (Windows Management Instrumentation), and ADSI (Active Directory Services Interfaces), WSH is a powerful tool that can be used for almost all of your scripting needs.



 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows