VBScript – Various Checks

There 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.

' Filename: checks.vbs 
' 
' Author: Br. David Carlson 
' 
' Date: August 5, 2001 
' 
' This WSH script checks to see if the user is Administrator. 
' If so, the script says so and quits. Otherwise, the script makes 
' a futher to check to see if Wscript was used to run the script. 
' If so, a message is echoed. If not, the script prints an error 
' message and quits. If all of these checks are passed, the script 
' then reports the values of a few environment variables. 

Dim scripthost 
Dim net 
Dim shell 
Dim environment 

set net = Wscript.CreateObject("Wscript.Network") 

if net.UserName = "Administrator" then Wscript.echo "Logged in as Administrator. Script will abort." 
Wscript.Quit 1 
' 0 is used for normal program exit 
end if 

scripthost = Wscript.FullName Wscript.echo scripthost 
if instr(1, scripthost, "Wscript.exe", vbTextCompare) = 0 then Wscript.echo "Script is NOT being run by Wscript. Script is aborting." 
Wscript.Quit 2 
else Wscript.echo "Script is being run by Wscript." 
end if 

set shell = Wscript.CreateObject("Wscript.Shell") 
set environment = shell.Environment Wscript.echo "Path: " & environment("PATH") & vbCRLF _ 
& "Processor: " & environment("PROCESSOR_ARCHITECTURE") & vbCRLF _ 
& "Operating system: " & environment("OS") & vbCRLF

The first check looks to see if Administrator is logged in.

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.

cscript checks.vbs 
wscript checks.vbs 

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.

SOURCE

LINK

LANGUAGE
ENGLISH