Running Scripts from the Command Line

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

Although this might be the age of the graphical user interface, many system administrators are still more comfortable working from the command prompt than within the GUI. This is not a problem with WSH; you can run scripts from the command prompt or from the GUI. Not only do you not lose any capabilities by choosing the command prompt over the GUI, but running scripts from the command line and under CScript also has at least two benefits:

  • It is easier to pass arguments to the script. These arguments might be used by the script itself (for example, you might pass the name of a folder to be deleted), or the script host might use them when running the script.
  • It is easier to cancel a script running under CScript. When a script runs from the command prompt, you can cancel the script either by pressing CTRL+C or by closing the command window in which it is running. If a script is running under WScript, the only way to cancel it is to terminate the Wscript.exe process.

You can run script files from the command line in one of two ways:

  • Type the name of the script, including its file name extension, at the command prompt:
  • HardwareAudit.vbs

  • Type the name of one of the script hosts followed by the name of the script:

    cscript HardwareAudit.vbs

    wscript HardwareAudit.vbs

When you use the first method, the command interpreter must determine which script host to call. If you do not specify either CScript or WScript, the script will run under the default script host as configured on the computer. When you use the second method, you explicitly specify the script host under which the script should be run. The command interpreter runs cscript.exe or wscript.exe, whichever was specified, passing it the script file HardwareAudit.vbs.

Script Host Options

Both CScript and WScript accept a number of options that either affect how the script host will run a script or modify some aspect of the WSH environment. The two script hosts share a common set of options; CScript also has a few options, most notably //logo and //nologo, which have no effect in WScript.

When WSH is first installed, WScript is configured as the default script host. (If you do not specify either CScript or WScript when starting a script, WSH runs scripts using the default script host.) To set the default script host to CScript, type the following at the command prompt:

cscript //H:cscript

To reset WScript as the default script host, type this:

wscript //H:wscript

Table 3.2 lists a number of the more commonly used WSH options.

Table 3.2   Script Host Options

ParameterDescription
//BBatch mode; suppresses display of user prompts and script errors. For example, if your script includes messages displayed using Wscript.Echo, these messages will not appear when the script runs in Batch mode. Batch mode also suppresses the use of VBScript functions such as Msgbox.

The default is Interactive mode.

//DTurns on the Microsoft Script Debugger if this program is installed. The Script Debugger ships as part of Windows 2000, although it is not installed by default. The Script Debugger does not ship with Windows XP .

If the Script Debugger is not installed, no error will occur. Instead, the script will simply run.

//E:engineExecutes the script with the specified script engine. Among other things, this allows you to run scripts that use a custom file name extension. Without the //E argument, you can run only scripts that use registered file name extensions. For example, if you try to run this command:

cscript test.admin

You will receive this error message:

Input Error: There is no script engine for file extension ".admin".

To run a script that uses a custom file extension, include the //E argument:

cscript //E:vbscript test.admin

One advantage of using nonstandard file name extensions is that it guards against accidentally double-clicking a script and thus running something you really did not want to run.

This does not create a permanent association between the .admin file name extension and VBScript. Each time you run a script that uses a .admin file name extension, you will need to use the //E argument.

//H:CScript or //H:WScriptRegisters Cscript.exe or Wscript.exe as the default application for running scripts. When WSH is initially installed, WScript is set as the default script host.
//IInteractive mode; allows display of user prompts and script errors. This is the default mode and is the opposite of Batch mode.
//logoDisplays a logo when the script runs under CScript (this is the default setting for WSH). The logo, which appears prior to any of the output from the script, looks like this:
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2000. All rights reserved.
//nologoPrevents display of the logo at run time (by default, the logo is displayed).

The //nologo option is often used for scripts whose output is redirected to a text file. Suppressing the logo ensures that this information does not appear within the text file. This makes it easier to write scripts that parse the information found in the text file or that import the contents of the file to a database, because these scripts do not have to account for the logo.

//SSaves the Timeout and Logo options for this user. For example, this command ensures that the logo will be suppressed anytime a script runs under CScript:

cscript //nologo //S

You can also modify these settings by right-clicking a script file and then clicking Properties.

//T:nnDetermines the maximum number of seconds the script can run. (The default is no limit.) The //T parameter prevents excessive execution of scripts by setting a timer. When execution time exceeds the specified value, the script host interrupts the script engine and terminates the process.
//XStarts the program in the Microsoft Script Debugger. If the Script Debugger is not installed, the script simply runs.
//?Displays a brief description of command parameters (the usage information). The usage information is similar to the information presented in this table, although with less explanation. For example, here is the usage information for the //E argument:
//E:engine Use engine for executing script

Redirecting Script Output to a Text File

Sometimes you run a script because you need to do something right away. For example, you might need to check the status of a particular service or the amount of free space on a particular hard drive. Other times you run a script with the intention of going back and analyzing the data later; for example, you might run a script that retrieves a list of all the software installed on all your domain controllers. Sometime in the future, you will examine that list and determine whether your domain controllers have been properly configured.

If your script retrieves data that needs to be accessed later on, it is a good idea to save this data, perhaps in a database or a text file. It is possible to include code within a script that saves data in either of these formats. If a script is designed to always save data, it is best to include the code to carry out that procedure.

But what if there are times when you want to save the output from a script and other times when, using that same script, you prefer to view that output on the screen? If you use Wscript.Echo to display data, you actually have two choices: display the data on the screen or write the data to a text file. If you choose to write the data to a text file, all you have to do is run the script using one of the two command-line redirection characters.

The command interpreter provides two ways to redirect output from the screen to a file. (That is, output is saved to a file instead of being displayed on the screen.) If you use the > character followed by a file name, output will be saved in a text file, overwriting anything that might already be saved in that file. For example, this command saves the output of a script to the file c:\scripts\services.txt:

cscript service_info.vbs > c:\scripts\services.txt

The >> characters append data to the specified file; new output from the script is added to anything already in the file:

cscript service_info.vbs >> c:\scripts\services.txt

You might also want to use the //nologo option, to ensure that the logo is not included within the text file:

cscript //nologo service_info.vbs > c:\scripts\services.txt

When you redirect the output of the script, no messages of any kind appear in the command window. Instead, all output, including error messages, is redirected to the text file.


send us your feedback Send us your feedback « Previous | Next »   


Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 635

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