The Ultimate Windows Server 1002003 System Administrators Guide [Electronic resources]

Robert Williams, Mark Walla

نسخه متنی -صفحه : 158/ 140
نمايش فراداده

WINDOWS SCRIPTING HOST

Windows Scripting Host (WSH 2.0) is a language-independent scripting engine used to automate administrative tasks, such as controlling network connections. WSH supports Windows 2000, Windows NT, and Windows 9x environments. The earlier version of WSH supported only Microsoft languages, such as Visual Basic VBScripts and JScript.

Microsoft and third parties are now providing WSH 2.0 with an augmented list of scripts to support other environments, including Visual Basic, Java, HTML, Perl, and XML. Previously, only Windows native scripts such as the MS-DOS command language were supported.

NOTEAn abundance of scripts is available on the Internet and from specialized programming books. We have included only a few scripts here because they tend to be enterprise-specific or task-specific. Instead, we provide an overview of WSH that should give you sufficient information to host your own scripts.

For readers who would like to create scripts, we recommend the Microsoft Windows Scripting Host Programmer's Reference site. A number of sample scripts can be downloaded from http://www.microsoft.com/scripting.

Using WSH to Run Scripts

There is both a Windows version and a command-line version of WSH. The Windows version (wscript.exe) provides the standard GUI interface to set properties. The command-line version (cscript.exe) uses switches to do this.

RUNNING WSCRIPT.EXE

Scripts can be launched within the Windows environment in several ways. In Windows Explorer, simply double-click the icon identified as a script, as shown in Figure 17.10. Alternatively, from the Start Run command line or the command prompt, enter the name of the script and click OK or Enter, respectively, as shown in Figure 17.11.

Figure 17.10. Launching a Script from Explorer

Figure 17.11. Launching a Script from the Run Command Line

Setting Properties for the Wscript.exe Environment

Two properties can be set for a Wscript.exe-launched script. From Windows Explorer right-click the targeted script and select Properties, where you will see two tabs for scripts residing on a FAT/FAT 32 partition and four available for NTFScommon to both are the General and Script tabs. From the General tab, you can set the script to assume a readable or hidden attribute. From the Script tab, you can automatically stop the script after the specified (nn) number of seconds, and you can display a logo banner. The two additional NTFS partition tabs are Security for setting user permissions and Summary, as shown in Figure 17.12.

Figure 17.12. The NTFS Partition Summary Properties Tab

When properties are set on a script, a text file with the .wsh extension on its name is created as a control file for the script's settings. The .wsh file uses the same name as the script and can also be used to launch the script in the manner just described.

Running Cscript.exe

The command-line version of WSH is launched from the command prompt. It uses the following syntax:

Cscript [script name] [host options] [script options]

The only required item is the script name. The host options enable or disable WSH options. The script options are passed directly to the script and are preceded by a single slash (/).

Table 17.1 lists the functions available for the Cscript.exe tool.

Table 17.1. Cscript.exe Host Parameters

Option

Description

//I

The default setting; permits interactive prompts and the display of errors.

//B

Batch mode; suppresses command displays and user prompts.

//D

Enables the debugger.

//E:engine

Specifies the script language engine.

//Job:xxx

Specifies a batch script to execute.

//T:nn

Enables a time-out on script execution after (nn) seconds.

//logo

The default; displays a logo banner.

//nolog

Suppresses the logo banner.

//H:C script or //H:W script

Registers which version of WSH is the defaultC for cscript, W for wscript.

//S

Saves the command-line options.

/?

Displays command usage.

SAMPLE WSH SCRIPTS

Example 1: Programming with the Windows Scripting Host allows direct access to the Active Directory via Active Directory Services Interface (ADSI).

'Display Objects in the Engineering Organizational Unit
Set ou = GetObject ("LDAP://OU=Engineering,DC=Entcert1,DC=Com")
For each obj in ou
WScript.Echo obj.Name
Next

Example 2: Properties relating to the Scripting Engine and executing script are accessible through script and argument properties.

'Display Script Properties
wscript.Echo Wscript.Application
wscript.Echo Wscript.Arguments(0)
WScript.Echo Wscript.FullName
WScript.Echo Wscript.Name
WScript.Echo Wscript.Path
WScript.Echo Wscript.ScriptFullName
WScript.Echo Wscript.ScriptName
WScript.Echo Wscript.Version
' Display script argument properties
WScript.Echo Wscript.arguments.Count
WScript.Echo Wscript.arguments.length
WScript.Echo Wscript.arguments.Item

Example 3: Environmental parameters from four environments are accessible by setting the environment context.

'Create shell object
Set WshShell = Wscript.CreateObject("Wscript.Shell")
' Set variables environmental variable access
' of system, User, Volatile, process
Set SystemEnvironment = WshShell.Environment("System")
Set UserEnvironment = WshShell.Environment("User")
Set VolatileEnvironment = WshShell.Environment("Volatile")
Set ProcessEnvironment = WshShell.Environment("Process")
' Display example parameters from each environment set
wscript.Echo SystemEnvironment("PATH")
wscript.Echo UserEnvironment("PROMPT")
wscript.Echo VolatileEnvironment("OS")
wscript.Echo ProcessEnvironment("PROCESSOR_ARCHITECTURE")

Example 4: Advanced dialog window capabilities are available and based on Win32 user interface programming. The Registry may be read and modified through shell methods.

' Registry reading and writing
Set WshShell = Wscript.CreateObject("Wscript.Shell")
' Simple Popup window
WshShell.Popup "Test Popup Window"
' Popup Window with advanced options following Win32 conventions
WshShell.Popup "Test Popup Window",10, "SysAdmin Tool", 4
' Read from the systems registry
str = WshShell.RegRead ("HKCU\Environment\TEMP")
WshShell.Popup str

Example 5: The network functions allow drives and printers to be mapped to users and systems throughout the network.

' Network methods can be used to add and remove printers and network drives.
Set WshNetwork = Wscript.CreateObject("Wscript.Network")
' Print out computer name
Wscript.echo WshNetwork.ComputerName
'Add a printer
WshNetwork.AddPrinterConnection"Laser"."\\ecc3s\HP Printer"
'Map a network drive
WshNetwork.MapNetWorkDrive "M:","\\ecc3s\aaECC"
'Display Username and domain
Wscript.echo WshNetwork.Username
Wscript.echo WshNetwork.UserDomain

Example 6: Shortcuts can be added to the desktop and assigned properties.

' Shortcut properties allow the modification of a shortcut's arguments, Description
' Hotkey, Iconlocation, TargetPath,WindowStyle and WorkingDirectory.
'
Set WshShell = Wscript.CreateObject("Wscript.WshShell")
strFavoritespath = WshShell.SpecialFolders("My Documents")
Set NewShortcut = WshShell.CreateShortcut(strFavoritespath & "\short.lnk)
NewShortcut.TargetPath = "path to application here"
NewShortcut.Save