Logon Scripts Using Windows Script Host


Logon Scripts Using Windows Script Host

In the long term , batch-processing files are not the solution of choice for complex scripting tasks. Therefore, Microsoft developed the Windows Script Host (WSH) concept. The WSH technology was introduced with Windows 2000 and seamlessly fit into the Microsoft component architecture strategy before the .NET Framework was introduced. Windows Server 2003 comes with Windows Script Host 5.6 and can therefore easily handle complex administrative tasks .

The command prompt uses the WSH and the existing Component Object Model (COM) interfaces to automate applications. The underlying script interpreters have been in use since Microsoft Internet Explorer version 3.0, particularly the two language dialects, Microsoft Visual Basic Script (VBScript) and JScript (a variant of Netscape JavaScript). Third-party manufacturers offer additional WSH scripting languages.

To execute scripts, systemwide available COM components are used. The Windows Server 2003 operating system manages and provides these components. Communication is handled by COM interfaces as in many conventional applications.

The COM components are based on different libraries that define constants, functions, objects, and events that, in turn , can be used by other applications or scripts. In this way, these scripting engines supply functions for interpreting scripts in a defined language syntax. Scripting engines are realized as ActiveX components that can be used by the Windows Script Host.

WSH Runtime Environment

The command prompt s script language can easily be expanded with this concept. The user or administrator uses object-oriented options for generating powerful console scripts and for controlling the entire user interface with command-line programs.

Windows Script Host can either run in a Windows mode (Wscript.exe) or in a command-line mode (Cscript.exe). There are three ways to run scripts with corresponding file extensions on a terminal server:

  • Double-clicking script files or their icons on the desktop

  • Using the Run... option in the Start menu by entering the complete script name

  • Executing Wscript.exe via the Run... option and the optional entry of parameters and arguments

If you run scripts in the command-line “based mode, use the following syntax:

 Cscript [Parameters] Scriptname.Extension [Options] [Arguments] 

The host parameters enable or disable different Windows Script Host options. These parameters always begin with a double slash ( // ). The script name is indicated by its extension ”for example, Chart.vbs for a Visual Basic script. Script parameters (options and arguments) always begin with a single slash ( / ).

The most important host parameters of the Cscript command are listed in Table 7.6.

Table 7.6: Host Parameters of the Cscript Command

Parameters

Description

//I

Interactive mode ( opposite of //B).

//B

Batch mode. Suppresses display of script errors and user queries.

//T:nn

Time limit in seconds. Maximum time for running a script (default value: unlimited). Suppresses uncontrolled script processes for an extended period of time.

//logo

Shows a WSH logo message at execution time (default setting, opposite of //nologo ).

//nologo

Suppresses display of WSH logo message.

//H:Cscript or Wscript

Defines Cscript or Wscript as the default application to run scripts (default setting: Wscript).

//S

Saves the current command-line options for the current user.

//D

Enables debug mode.

//X

Executes the script in the debugger.

//job:

Specifies a job that is executed within the script. Different jobs are defined in XML syntax.

//?

Displays help information. Corresponds to running Cscript without parameters.

The easiest way to generate scripts is by using Notepad, but Microsoft Script Debugger and Microsoft Script Editor are much more powerful.

Object-Oriented Programming Model

What exactly is an object? This is the question many administrators ponder if they have never had to do object-oriented programming before. First of all, an object is simply a thing with a set of characteristics and attributes that describe the object. In principle, objects are divided into several categories, all relating to different units: users, groups, computers, printers, networks, or applications. Each of these objects has a set of properties that describes it in detail. Furthermore, each object can perform actions using methods. You might, for instance, look at a user object with these properties: user name, ID, and password. It might have methods to create, delete, or modify passwords. However, you need to create the user object before you can interact with it.

Before accessing an object s properties and methods, a corresponding instance needs to be created using two methods provided in the WSH runtime environment: CreateObject and GetObject . The concept of calling up a method within an object is radically different from using tools in batch-processing scripts as described previously. CreateObject is also a function in VBScript, which somewhat simplifies its use. As a result, the easiest syntax for creating an object instance is this:

 set ObjectRef = CreateObject (strApp.strObject [, strServerName"]) 

The CreateObject function returns the object instance that is to be used. The most important parameter for creating the object instance is ProgID ( strApp.strObject , for example Scripting.FileSystemObject or Wscript.Shell ). Using both object reference and point operator, the appropriate method can be found.

 ObjectRef.ObjectMethod Parameter1, Parameter2 

A special Wscript object library that comes with Windows Script Host allows access to the registry, file links, system folders, system properties, network drives , and network printers. The library can be invoked in the WSH Windows mode using Wscript and in the command-line mode using Cscript . Most of its objects are limited to the computer where the WSH script runs. This is why Windows Script Host is particularly suitable for logon scripts and standardized administration tasks.

The Wscript object has a number of properties and methods. These relate to Windows Script Host or the script being executed. Table 7.7 lists the most important methods in alphabetical order.

Table 7.7: The Most Important Wscript Object Methods

Methods

Description

AddWindowsPrinter

Adds printers

CreateObject

Creates an object and sets up system event processing

DisconnectObject

Disconnects an object from Windows Script Host

Echo

Displays the parameters in a window or in the command prompt

GetObject

Gets an automation object from a file

Sleep

Stops the script for the duration of n seconds

Sendkey

Controls older applications that do not have a COM interface by simulating keyboard input

StdIn/StdOut

Supports input and output via the command prompt

Quit

Ends the script execution

The Windows Script Host environment has additional COM-based libraries: WshNetwork , WshShell , and WshController . WshNetwork is used for network and printer connection access, while WshShell is suited for access to the desktop, environment variables , and the registry. The Collection , Environment , Shortcut , SpecialFolder , and UrlShortcut objects are not directly available. Instead, they are supplied by the methods of the other objects. Special Windows Server 2003 objects allow access to the Active Directory system information (ADSI) and Windows Management Instrumentation (WMI). The properties and methods of all these objects represent the entire functionality of Windows Script Hosts. Expansions through additional COM automation objects are possible at any time.

Note  

Using Windows Script Host requires solid knowledge of object-oriented programming. Normally, VBScript is the program language that is easiest to understand for beginners . JScript, on the other hand, is very well suited for administrators who are already familiar with Java, C, or C++. The objects available on the system, including all methods, can be analyzed with the OleView.exe tool.

Examples

In the following section, we will look at some WSH scripts that provide simple functions. Building on that information, new scripts can be created ”for instance, for user logon, file administration, and adjusting the registry on a terminal server. In particular, we will highlight the possibility of performing very complex manipulations of character strings and modifying a user s desktop settings.

Environment Variables

The Showvar.vbs script is used for displaying all environment variables. The shell object is needed to determine the data, and the echo method is needed to display the result.

Listing 7-9: The Showvar.vbs WSH Script
start example
 


Microsoft Windows Server 2003 Terminal Services
Microsoft Windows Server 2003 Terminal Services
ISBN: 0735619042
EAN: 2147483647
Year: 2004
Pages: 119

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net