Windows Scripting Host (WSH) is a Window administration tool built into the Windows operating system (OS). WSH can run many different scripting languages including the following:

  • VBScript
  • JavaScript/JScript
  • PERL
  • PerlScript
  • REXX
  • Python

Here are the different versions of WSH available:

  • 1.0 is the default for Windows 95, 98, and NT 4.0.
  • 2.0 is the default for Window 2000 and ME.
  • 5.6 is available for download from Microsoft.

Scripts are ideal for short, sequential, and non-interactive tasks. Here are some of the various stand alone scripts:

  • .bat. MS-DOS batch file. Runs OS script.
  • .asp. ASP page file. Can run server or client side script.
  • .pl. PERL file.
  • .html. HTML web file. Can run client side script.
  • .js. JavaScript/JScript file. Included for client or server side. Runs OS script.
  • .vbs. VBScript file. Included for client or server side. Runs OS script.
  • .wsf. WSH file. Runs OS script. Can include script written in different languages either internal or external to the WSH file. A WSH file is an XML document (Extensible Markup Language) that can be edited with a plain text editor such as Notepad.

Search MSDN Microsoft for things like wsh and Basic Windows Script Host Tasks.

WSH Files

Here are some of the basic things a WSH file can do:

  • Have scripts written in different languages internal to the file.
  • Include, ie point to, other scripts that are external to the file.
  • Have several jobs where each job can be executed separately if needed.
  • Reference COM objects.
  • Since it is an XML document (Extensible Markup Language), it can be edited with a plain text editor such as Notepad.

WScript.exe returns results to a window, whereas CScript.exe returns results to a command line. Running a WSH file by double click it will run the WScript.exe. Running a WSH file from a command line will run either WScript.exe or CScript.exe.

EG

Place the following into plain text files called zgh.wsf, zgh.js, and zgh.vbs respectively.

<package>
<job id="j1">
    <?job debug="false" error="false" ?>
    <script language="JScript" src="zgh.js" />
    <script language="VBScript">
        s = GetFreeSpace("c:")
        WScript.Echo s
    </script>
</job>
<job id="j2">
    <?job debug="false" error="false" ?>
    <script language="VBScript" src="zgh.vbs" />
    <script language="VBScript">
        s = ReportFileStatus("C:\zgh.js")
        WScript.Echo s
    </script>
</job>
</package>
function GetFreeSpace(DirPath) {
    var fs, d, s;
    fs = new ActiveXObject("Scripting.FileSystemObject");
    d = fs.GetDrive(fs.GetDriveName(DirPath));
    s = "Drive " + DirPath + " - " ;
    s += d.VolumeName;
    s += " Free Space: " + d.FreeSpace/1024 + " Kbytes";
    return s;
} 
Function ReportFileStatus(FileSpec)
    Dim fso, msg
    Set fso = CreateObject("Scripting.FileSystemObject")
    If (fso.FileExists(FileSpec)) Then
        msg = FileSpec & " exists."
    Else
        msg = FileSpec & " doesn't exist."
    End If
    ReportFileStatus = msg
End Function

Run the WSH file at a command line:

CScript zgh.wsf //job:j2 //job:j1

Jobs can be run in any order. If no job is specified, the first one is run. If the WSH file contained only one job, then the <package> element is not necessary.

EG

This is an example of a wsh that I might run at startup.

<job id="j1">
<script language="VBScript">

Dim oShell
Set oShell = WScript.CreateObject("WScript.shell")

'oShell.run "C:\WINNT\explorer"
'WScript.Sleep 3000

oShell.run """C:\Program Files\Microsoft Office\Office10\FRONTPG.EXE"" C:\Inetpub\wwwroot\pvtGH"
WScript.Sleep 14000

oShell.run """C:\Program Files\Microsoft Office\Office10\FRONTPG.EXE"" C:\Inetpub\wwwroot\wwwGH"
WScript.Sleep 8000

oShell.run """c:\Program Files\Internet Explorer\IEXPLORE.EXE"" http://www.google.com/"
WScript.Sleep 3000

oShell.run """c:\Program Files\Internet Explorer\IEXPLORE.EXE"" http://mail.yahoo.com/"
WScript.Sleep 3000

oShell.run """c:\Program Files\Internet Explorer\IEXPLORE.EXE"" http://localhost/wwwGH/Index.htm"
WScript.Sleep 3000

oShell.run """C:\WINNT\NOTEPAD.EXE"" ""C:\Documents and Settings\Administrator\Desktop\aaDesk.txt"""
WScript.Sleep 3000

Set oShell = Nothing

</script>
</job>


GeorgeHernandez.comSome rights reserved