|Windows Script Host (WSH)?|
What is Windows Script Host (WSH)?
In this post I will cover windows script host (wsh) tutorial in step by step manner.By technology,it is a program that runs within windows environment and acts as a container to run VbScript files.It is a Windows administration tool. WSH creates an environment for hosting scripts.That is, when a script arrives at your computer, WSH plays the part of the host .
It makes objects and services available for the script and provides a set of guidelines within which the script is executed. Among other things, Windows Script Host manages security and invokes the appropriate script engine. Windows Script Host is built into Microsoft Windows 98, 2000, and Millennium Editions and higher versions.
A Windows script is a text file. We can create a script with any text editor as long as we save our script with a WSH-compatible script extension (.js, vbs, or .wsf).WSH also supports to write scripts in JScript,Perl,Phython,REXX,other ActiveX scripting laguage including VBScript.
Refer here for more details for WSH. I am highly encouraged by this site. This post is a lighter version of the same mostly applicable for UFT.
by writing a WSH script we can take advantages of ADSI and WMI. We will discuss these within a while.
List of Supporting technologies
|.bat||MSDOS batch file|
|.wsh||Windows Script host files|
|.wsf||Windows Script host files|
Common Editor for WSH Scripts
The most commonly available text editor is already installed on our computer ? Notepad. We can also use your favorite HTML editor, VbsEdit, Microsoft Visual C++, or Visual InterDev.
Below are the popular VBScript /VBS editors
From where to download WSH
WSH can be downloaded from here.
How to Write Code for WSH or VBScript or VBS
Creating a script with Notepad
- Start Notepad.
- Write your script. For example purposes, type Msgbox “Hello VB Script”
- Save this text file with a .vbs extension (instead of the default .txt extension). For example, Hello.vbs
- Navigate to the file you just saved, and double-click it.
- Windows Script Host invokes the VB Script engine and runs your script. In the example, a message box is displayed with the message “Hello VB Script”
- Hosting Environments and Script Engines
Scripts are often embedded in Web pages, either in an HTML page (on the client side) or in an ASP page (on the server side).In the case of a script embedded in an HTML page, the engine component that interprets and runs the script code is loaded by the Web browser, such as Internet Explorer.In the case of a script embedded in an ASP page, the engine that interprets and runs the script code is built into Internet Information Services (IIS).Windows Script Host executes scripts that exist outside an HTML or ASP page and that stand on their own as text files.
How to Run WSH Scripts
- Using commandline. It uses Cscript.exe . It is used in MSDOS console window.
- Within Windows like double click on the script file.It uses WScript.exe. It is used in Windows GUI.
we can pass arguments to the Cscript as well:
If we run CScript.exe without any arguments , directly from command line, we can get the usage notes.
The switches available to run a Cscripts are as follows:
|//B||Batch mode on Suppress the errors and prompts|
|//E:engine||Executes scripts by using engine|
|//H:Cscript||Changes the host to Csript|
|//H:Wscript||Changes the host to Wscript|
|//I||Activates interactive mode|
|//Job:XXX||Executes a WSF job|
|//Logo||Activates and display a Logo|
|//NoLogo||Deactivate logo display|
|//S||Saves the command line options for active user|
|//T:nn||Sets time out in Seconds. A script can run ‘nn’ seconds. Post this time out the script just aborts|
|//X||Run the script in Debugger mode|
|//U||Activates Unicode for redirecting IO from console|
Execute WSH using WScript.exe
Similarly we can execute WSH scripts in Windows environment using WScript.exe.
- The execution begins once we double click on the file.In this case the file has to be registered with WSH.
- We can also use Run command with full path of the script and scriptname
However if we run WScript from command prompt, instead of getting output, we will get a configuration box for script customization options.If Ok is clicked after providing all customization options, It wont run. It just saves the individual configurations for the script.
What is the difference between CScript.Exe and WScript.Exe?
The difference comes to an existence when we debug a script. CScript.exe sends the error message directly to the console which is very convenient to use.Where as WScript.exe shows the errors in the form of popup. So handling huge number of popups would be tedious.
So while debugging we need to use CScript.exe and while running we need to use WScript.exe with Echo() method enabled.
How to create a .WSH file?
In order to create a .WSh file, we select a file that is currently associated with WScript(.js,.VBS,.wsf etc) then right click on that.
Once the Properties dialog opens, navigate to different tabs. It will allow us to change the default configurations.We can make changes to them and can apply the changes. This will create a new .wsh file with the same script name.
It will be like below:
[ScriptFile] Path=D:\test\myTest.vbs [Options] Timeout=30 ... ...