web analytics
Windows Script Host: CScript.exe and WScript.exe Options
davegate
Posted: Wednesday, December 23, 2015 6:28:44 PM

Rank:Advanced Member
Groups: Member
Joined: 12/14/2015
Posts: 182
Points: 894

Microsoft Windows Script Host (WSH) is a language-independent scripting host for Windows Script compatible scripting engines. It brings simple, powerful, and flexible scripting to the Windows platform, allowing you to run scripts from both the Windows desktop and the command prompt.  Windows Script Host has very low memory requirements, so it is ideal for both interactive and non-interactive scripting needs, such as logon scripting and administrative scripting.

There are two versions of WSH: a Windows-based version (Wscript.exe) that provides Windows-based properties for setting script properties, and a command prompt-based version (Cscript.exe) that provides command-line switches for setting script properties. For example, if you use Cscript your output from a Wscript.Echo statement will, by default, display to the command window; if you use Wscript that output will display in message boxes.

You can run either of these versions by typing "wscript.exe" or "cscript.exe" at a command prompt.

Windows Script Host supports scripts written in VBScript or JScript. When you start a script from your desktop or from the command prompt, the script host reads and passes the specified script file contents to the registered script engine. The script engine uses file extensions (that is, .vbs for VBScript and .js for JScript) to identify the script. As a result, you do not need to know the exact programmatic identifier (that is, the ProgID) of the script engine. The script host maintains a mapping of script extensions to programmatic identifiers, and uses the Windows association model to start the appropriate engine for a given script.

Sponsor
Posted: Wednesday, December 23, 2015 6:28:44 PM
davegate
Posted: Wednesday, December 23, 2015 6:44:20 PM

Rank:Advanced Member
Groups: Member
Joined: 12/14/2015
Posts: 182
Points: 894

Creating Scripts That Can Be Used by WSH

A Windows script is a text file. You can create a script with any text editor as long as you save your script with a WSH-compatible script extension (.js, vbs, or .wsf).

For example, the following instructions shows you how to create a simple script with Notepad

  1. Start Notepad.
  2. Write your script. For example purposes, type WScript.Echo("Hello World!");
  3. Save this text file with a .js extension (instead of the default .txt extension). For example, Hello.js.
  4. Navigate to the file you just saved, and double-click it. (You may have to specify the Windows Script Host as the application to use.)
  5. Windows Script Host invokes the JScript engine and runs your script. In the example, a message box is displayed with the message "Hello World!"

davegate
Posted: Wednesday, December 23, 2015 6:55:56 PM

Rank:Advanced Member
Groups: Member
Joined: 12/14/2015
Posts: 182
Points: 894

Setting the Default Script Host

When you double-click the filename with the .vbs extension in Windows Explorer, Windows will automatically run it using using the Wscript script host. If you want to change your default script host, you have to do that by typing this at the command prompt:

cscript //H:cscript

After running this command, if you type only the name of the script file at the command prompt, the script will run under Cscript rather than Wscript (because CScript is now the default script host).

 

To set the default back to Wscript, simply run this command:

wscript //H:wscript
davegate
Posted: Wednesday, December 23, 2015 7:07:00 PM

Rank:Advanced Member
Groups: Member
Joined: 12/14/2015
Posts: 182
Points: 894

CScript.exe: Command-line based Script Host

Cscript.exe is a command-line version of the Windows Script Host (WSH) that provides command-line options for setting script properties.

With Cscript.exe, you can run scripts by typing the name of a script file at the command prompt.

 You can run scripts with the command-line-based script host by typing the following at the command prompt:

cscript [script name] [host options] [script arguments]

Where:

  • script name is the name of the script file, including the file name extension and any necessary path information.
  • host options are the command-line switches that enable or disable various Windows Script Host features. Host options are always preceded by two slashes (//).

  • script arguments are the command-line switches that are passed to the script. Script arguments are always preceded by one slash (/).

Note

Each parameter is optional; however, you cannot specify script arguments without specifying a script. If you do not specify a script or any script arguments, Cscript.exe displays the Cscript.exe syntax and the valid host options.

The command-line-based script host supports the following host options:

Parameter Action

//B

Specifies batch mode, which does not display alerts, scripting errors, or input prompts.

//D

Turns on the debugger.

//E:engine

Specifies the scripting language that is used to run the script.

//H:cscript or //H:wscript

Registers either Cscript.exe or Wscript.exe as the default script host for running scripts. If neither is specified, the default is Wscript.exe.

//I

Specifies interactive mode, which displays alerts, scripting errors, and input prompts. This is the default and the opposite of //B.

//Job:xxxx

Runs the job identified by xxxx in a .wsf script file.

//Logo

Specifies that the Windows Script Host banner is displayed in the console window before the script runs. This is the default and the opposite of //Nologo.

//Nologo

Specifies that the Windows Script Host banner is not displayed before the script runs.

//S

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

//T:nnnnn

Specifies the maximum time the script can run (in seconds). You can specify up to 32,767 seconds. The default is no time limit.

//X

Starts the script in the debugger.

//? 

Displays available command parameters and provides help for using them (this is the same as typing Cscript.exe with no parameters and no script).

 

The time out option (//T:nnnnn) prevents excessive execution of scripts by setting a time limit. When execution time exceeds the specified value, Cscript.exe interrupts the script engine and stops the process.

You can also use Windows Script Host to create .wsf script files, with which you can call multiple scripting engines and perform multiple jobs, all from one file.

Users browsing this topic
Guest

Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.


© 2018 Digcode.com. All rights reserved.