Various Checks in WSHThere are many checks that can be made in Windows Script Host. For example, you can check how many processors your computer has, if the path contains a certain directory, what the network login name is, whether Cscript or Wscript was used to run the script, etc. The following script tries out a number of these. Note that the values of the environment variables displayed at the end could have been used as the basis for further checks, such as whether the path contained C:\Util, for example.
The first check looks to see if Administrator is logged in. This was previously explained in the section on Mapping a Drive and Starting an Application and so will not be examined further here. Wscript.Quit is used to exit from the script. A default value of 0 is returned unless you supply a number such as the 1 used in this script. A 0 indicates normal return; anything else indicates an error. Next, the FullName property of the Wscript object is checked. FullName is the complete path to the script host that is running the script. Wscript has other useful properties that can be checked, such as Path (the path to the script itself) and ScriptName (the name of the file containing the script). The instr function is used to find out if "Wscript.exe" is a substring of FullName. Note that a case-insensitive text comparison is used. The first parameter of 1 specifies that the search for the substring starts at the first position in the second parameter (FullName essentially). The instr function returns a value of 0 if the desired substring was not found. It returns the index of the start of the substring within the larger string if the substring was found. To try out the checks.vbs script to see if it correctly discovers which script host is running it, Cscript or Wscript, go to the command prompt and enter each of the following. It is assumed that the checks.vbs script is in the current directory.
The Environment property of Wscript.Shell is an object that gives you
access to the environment. In particular, the checks.vbs script uses
it to look up the path, processor, and operating system. When these values
are echoed, the command spans several lines. To handle this, the underscore
character is placed at the end of each line that has to be continued onto
the next line. In other words, underscore is the line continuation character.
More InformationThere is also a SpecialFolders property for Wscript.Shell that gives you the names of various special folders, such as the StartUp folder and the Desktop folder. For further information see the WSH References section. Back to main Windows Script Host page |