Obtaining WSH Environment Information
Microsoft® Windows® 2000 Scripting Guide
The WScript object has a number of properties that provide information about the script host being used and the script being run. This metadata includes the information shown in Table 3.7.
Table 3.7 WSH Environment Properties
Property |
Description |
---|---|
ScriptFullName |
Full path of the currently running script (for example, C:\Scripts\Monitor.vbs). |
ScriptName |
File name of the currently running script (for example, Monitor.vbs). |
Version |
WSH version number (for example, 5.6). |
Build |
WSH build number. The full version number of Windows Script Host consists of the product release version number followed by the build version number. For example, if the Windows Script Host product release version number is 5.6, and the build version number is 6626, the full version number is 5.6.6626. The build number is perhaps most useful if you are testing beta copies of WSH. Otherwise, the version number will typically suffice. |
Name |
Always returns Windows Script Host. |
FullName |
Full path to the script host (either Wscript.exe or CScript.exe). |
Path |
Full path to the folder where the script host is located. Does not include the file name (Wscript.exe or Cscript.exe). |
This metadata can be extremely useful. For example, the version number can be used to verify that a computer has the correct version of WSH installed; if you have a script that requires WSH 5.6, the script can first check the version number and then terminate if Version is not 5.6. For example:
If Wscript.Version <> "5.6" Then
Wscript.Echo "This script must be run under WSH 5.6."
Wscript.Quit
End If
Likewise, you might have a script that makes extensive use of Wscript.Echo. In that case, you probably do not want to run the script under WScript; this can result in hundreds of message boxes that must be responded to. Consequently, your script might check to see which script host it is running under and then terminate if the host is not CScript.
You can determine the script host by using the FullName property and using the VBScript function Right to check the last 11 characters. (To ensure that the check is done correctly, you should also convert these 11 characters to uppercase using the UCase function.) If the last 11 characters equal WSCRIPT.EXE, the script is running under WScript. If the last 11 characters are CSCRIPT.EXE, the script is running under CScript.
Note
- Why the last 11 characters? The value of ScriptFullName will vary depending on where WSH has been installed; it might be C:\Winnt\System32\CScript.exe, or it might be E:\Windows\System32\Wscript.exe. However, the last 11 characters in the path will always be either Wscript.exe or Cscript.exe.
For example, the following script snippet checks the script host and then displays a message and terminates if the host is WSCRIPT.EXE:
If UCase(Right(Wscript.FullName, 11)) = "WSCRIPT.EXE" Then
Wscript.Echo "This script must be run under CScript."
Wscript.Quit
End If
The script in Listing 3.14 displays the available WSH environment properties by retrieving them and echoing them to the screen.
Listing 3.14 Displaying Information About the WSH Environment
1 2 3 4 5 6 7 |
|
The following is typical output from the script:
Script Full Name: C:\scripts\wsh_info.vbs
Script Name: wsh_info.vbs
Version: 5.6
Build: 6626
Name: Windows Script Host
Full Name: C:\WINDOWS\System32\CScript.exe
Path: C:\WINDOWS\System32