Vous êtes sur la page 1sur 33

WSH

Windows Script Host


Programming environment that allows you to write & run

scripts that run on Windows Operating Systems. Scripts are small text based files written in English like programming language. They can be executed from the Windows command prompt or directly from the Windows desktop. Scripts provide quick & easy way to automate lengthy tasks that would take too much time or effort using the Windows GUI(graphical user interface).

WSH components
WSH is a 32-bit application that is made up of a number of

different components that include:


Script Engines Script Execution Hosts

WSH Core Object Model

Relationship between components


WSH Architecture VBScript Script Engine JScript Script Engine 3rd Party Script Engines

CScript Execution Host

WScript Execution Engine

WSH.OSX (Core Object Model)


Windows
Windows Desktop Windows Applications Quick Launch Bar Network Drives Etc Windows start Menu Windows File System Network Printers Windows Registry

WSH Scripting Engines


A script execution program is a program that processes the

statements that make up scripts & translate them into machine readable code that computer can understand & execute. WSH provides each script with a number of resources. WSH provides script engines for processing scripts. By default WSH can process scripts written in Jscript or VBScript. WSH is designed in a modular fashion. It allows Microsoft & third-party software developers to add support for additional scripting engines. Script Execution Engines have already been developed for Perl, Python, & Rexx

WSH Script Execution Host


To actually run a script, the WSH uses a script execution

host to process a script after the script engine has interpreted the script. WSH supplies two different script execution hosts:
Cscript.exe An execution host that enables script to execute from the Windows command prompt & display text based messages.
Wscript.exe An execution host that enables script to execute from the Windows desktop, display messages, & collect user input using graphical pop up dialogs.

WSH Script Execution Host


Both the execution hosts provide identical functionalities.
Wscript.exe provides the additional capability to display

graphical pop-up dialogs. In fact, the messages can still be displayed using the Cscript execution host. You can choose any of the execution host depending on your personal choice. However, if you wish to run your script in background & has no need to interact with the user, you should use Cscript.exe.

WSH Core Object Model


This model is critically important to the development &

execution of scripts. WSH core object model provides VBScript with direct access to windows resources. WSH Core Object Model provides access to the certain areas that include:
Windows desktop Windows Start Menu Windows Applications

Windows File System


Network printers Network drives Windows Registry

WSH Core Object Model


The Core Object Model exposes windows objects (file,

folder, disk drive, printer, etc) in a format that allows scripts to view, access & manipulate them. Each exposed object has associated properties & methods that scripts can use to interact with an object, as well as affect its behavior or status.

Interface between scripts & the operating system

Windows Shell

Windows Operating System

Windows Shell
A text based interface between user or scripts & the

operating system. Windows command prompt is used to communicate with the windows shell. The commands we enter are translated by windows shell into the format that the operating system can process. The operating system returns the results to the windows shell which displays them in the windows console.

Executing vbscript on command prompt


To execute the vbs script on command prompt, type
Cscript Scriptname.vbs

Windows run a script based on the authority of the person

who starts it.

WSH Core Object Model


To write scripts that will run on windows operating system,

you need to understand WSH Core object model. Understanding this model provides you with technical insights required to develop the script. This model provides programmatic access to windows resources. There are 14 objects in WSH Core object model. Each of these objects provides access to particular category of Windows resources.

WSH Core Object Model


WScript

WSH Arguments

WSH Controller

WSH Shell

WSH Network

WSHNamed

WSHUnnamed

WSHRemote

WshShortcut

WSHEnvironment

WshUrlShortcut

WSHSpecialFolder

WSHScriptExec

WSHRemoteError

Wscript
Wscript object is referred to as a public or exposed objects. Wscript Object provides access to a number of very useful methods. The properties & methods of Wscript object can be accessed directly

from within your script. The methods of Wscript object include:


CreateObject DisconnectObject

Establishes an instance of specified object Prevents a script from accessing a previously instantiated object
Echo

Displays a text message as a pop up dialog


Quit

Terminates a script execution


Sleep

Pauses the execution of a script for a specified number of seconds

Public Objects of WSH core object model


WSHController WSHShell WSHNetwork

Each of these objects must be instantiated within your script using the Wscript objects CreateObject method.

Methods Of Instantiation
Object WshArgument WshNamed WshUnnamed WshRemote WshRemoteError WshShortcut WshUrlShortcut WshEnvironment WshSpecialFolders WshScriptExec Method Of Instantiation Wscript.Arguments Wscript.Arguments.Unnamed Wscript.Arguments.Named WshController.CreateScript WshRemote.Error WshShell.CreateShortcut WshShell.CreateShortcut WshShell.Environment WshShell.SpecialFolders WshShell.Exec()

WScript
WSH Root Object Provides access to rest of the objects in the core model
Properties Arguments ScriptFullName StdOut Methods ConnectObject() CreateObject() DisconnectObject() Quit() Sleep() Echo() FullName ScriptName Version InteractiveName StdErr Path StdIn

WSHArguments
This method enables you to access command line

arguments passed to the script at execution time.


Properties Count UnNamed Methods Count() ShowUsage() Item Named Length

WSHNamed & WSHUnnamed


WSHNamed Provides access to a set of named command line arguments WSHUnnamed Provides access to a set of unnamed command line arguments
WSHNamed Properties Methods WSHUnnamed Properties Methods Item Count() Length Item Count() Length Exists()

WSHController
Provides the capability to create a remote script process
Properties Does not support any properties Methods CreateScript()

WSHRemoteError
Provides access to information on errors produced by

remote scripts.
Properties Description SourceText Methods Does not support any methods Line Source Character Number

WSHNetwork
Provides access to a number of different network resources

such as network printers or drives.


Properties ComputerName Methods AddWindowsPrinterConnecti on() EnumPrinterConnection() RemotePrinterConnection() AddPrinterConnection() MapNetworkDrive() SetDefaultPrinter() EnumNetworkDrives() RemoteNetworkDrive() UserDomain UserName

WSHShell
Provides access to windows registry, event log,

environment variables, shortcuts & Applications


Properties CurrentDirectory Methods AppActivate() LogEvent() RegRead() SendKeys() CreateShortcut() PopUp() RegWrite() Exec() ExpandEnvironmentStrings RegDelete() Run() Environment SpecialFolders

WSHShortcut
Provides script with methods & properties for creating &

manipulating windows shortcuts


Properties Arguments HotKey WindowStyle Methods Save() Description IconLocation WorkingDirectory FullName TargetPath

Special folders
VBScript can use some special folders to place shortcuts on. These include: Desktop
Represents Windows Desktop Programs Represents Program Menu under Windows Start Menu Start Menu Represents Start Menu

All these folders are user specific. Changes made to these special folders affect only the user that run the script that changes them. Any other user who is sharing your computer will not have his desktop affected.

Special Folder
To make the change for all the users logging on to a

computer, use the following folders:


AllUsersDesktop AllUsersStartMenu

AllUsersPrograms

WSHUrlShortcut
Provides script with methods & properties for creating &

manipulating URL shortcuts


Properties TargetPath Methods Save() FullName

WshEnvironment
This object provides access to windows environment

variables
Properties Item Methods Remove() Count() Length

WSHSpecialFolders
Provides access to special windows folders that allows

scripts to configure the start menu, Quick Launch Toolbar & other special Windows folders.
Properties Item Methods Count()

WSHScriptExec
Provides access to error information from scripts run using

the Exec method.


Properties Status Methods Terminate() StdErr StdOut StdIn

WshNetwork
Examples