- Tools for Creating Scripts
- Creating Reusable Scripts
- Linking Scripts
- The WSH Object Model
- Summary
The WSH Object Model
As we have mentioned previously, you access the functionality of Windows Script Host through the use of an object model. The object model provides the means to gain access to the properties and methods of the various pieces of WSH. Figure 3.7 shows a picture of the WSH object model for a reference.
Figure 3.7 A pictorial representation of the WSH object model used to access the features of WSH.
This object model shows 14 objects with the root object being the WScript object. These objects are actually COM interfaces. They can be divided into two categories, Script Execution and Troubleshooting and Helper Functions.
The first category enables you to create scripts that can manipulate the WSH; you can use the CreateObject and GetObject methods, and you can output messages to the screen. Figure 3.6 shows an example of screen output by using a message box.
The Help Functions category contains properties and methods that you use to manipulate the registry and access network shares or environment variables.
Table 3.1 shows a breakdown of the objects in the WSH object model and the various tasks that you can perform with each object.
Table 3.1 WSH Objects and Their Tasks
Object |
Task |
WScript |
Set and retrieve any command-line arguments Determine names of script files Determine whether CScript or WScript is the host environment Determine version information for the host being used Stop a script from executing Create COM objects as well as connect to and disconnect _from them Output information to the screen, such as with a message box |
WshArguments |
Use to access the command-line arguments |
WshNamed |
Use to access the named command-line arguments |
WshUnnamed |
Use to access the unnamed command-line arguments |
WshNetwork |
Connect to network shares such as drives and printers _(also used to disconnect from these devices) Map/unmap network drives Gain access to currently logged-on user |
WshController |
Uses the CreateScript method to create a remote script process |
WshRemote |
Manipulate scripts or programs Remotely administer network computers |
WshRemoteError |
Use to access remote script error information when that script terminates abnormally (as a result of an error) |
WshShell |
Run programs on the local computer Manipulate the registry Create shortcuts Gain access to the system folder Manipulate environment variables such as PATH |
WshShortcut |
Create shortcuts programmatically |
WshSpecialFolders |
Used to gain access to the Windows special folders |
WshUrlShortcut |
Create shortcuts to Internet resources |
WshEnvironment |
Access environment variables such as PATH or WINDIR |
WshScriptExec |
If you have run a script with Exec, use this object to determine status and error information about the script Gain access to stdIn, stdOut, and stdError |
Although these are the objects available for WSH, you are not limited to using just these objects. Any ActiveX control that exposes a COM interface can be accessed and manipulated with WSH and the scripts that you write. This includes the Active Directory running on Windows 2000/XP.
Although you will find a complete reference in Appendix A for the objects exposed in the WSH object model, we will offer some examples here as to how each object might be used _to help you gain a better understanding of each object. After you know how to work with _the objects at a basic level, you can apply that knowledge to other properties or methods of the individual objects. We will start with the root object WScript and work down through the object model.
Using the WScript Object
The WScript object does not have to be instantiated with a Set statement before you use it. Instantiating an object simply means that you declare a variable name to hold a reference to the object and then you assign that object to the variable with a Set statement. This is the only way you can access the object for manipulation. Once the object is instantiated, you can simply call the WScript object and any of its properties or methods directly. Listing 3.6 shows the simplest of all uses for the WScript object.
Listing 3.6 Using the WScript.Echo Method to Output Text to the Screen
Wscript.Echo ("Hello World!")If you type this script into your script editor of choice and execute it, you will see a message box display on your screen with the text Hello World! displayed.
This particular script works as either a VBScript or JScript file.
You can combine text and variables in an Echo method call as demonstrated by Listing 3.7.
Listing 3.7 Using a Variable to Determine the WSH Version Information and Then Concatenating the Variable and a Text String
Dim strVersion strVersion = WScript.Version WScript.Echo ("WSH Version is " & strVersion)
The output of this code snippet is shown in Figure 3.8.
Figure 3.8 A message box showing the results of running the code in Listing 3.7.
Using the WshArguments Object
The next object in the model is the WshArguments object. This object is used to access the list of command-line arguments that have been used in a script. The syntax for this usage can be best explained with a working example. Listing 3.8 shows the code to use.
Listing 3.8 VBScript Code to Be Used for Enumerating and Displaying Any Arguments Passed in at the Command Line
Set objArgs = WScript.Arguments For I = 0 to objArgs.Count 1 WScript.Echo objArgs(I) Next
Save this script as Arguments.vbs in a directory called code, then enter the following text at the command prompt:
Wscript d:\code\Arguments.vbs /a:"This is argument 1" /b:"This is argument 2"
You will see two successive message boxes indicating the arguments that you just entered. _This is known as accessing the named arguments. If you didn't give the arguments a name, they can still be displayed by issuing the following command at the command prompt:
Wscript d:\code\Arguments.vbs "Using an unnamed argument"
This will produce another message box with the text Using an unnamed argument in it, as shown in Figure 3.9. As you can see by these examples, the WshArguments object is used to enumerate the named or unnamed arguments passed to the script.
Figure 3.9 A message box showing the result of calling the script from the command prompt that doesn't use a named argument.
Using the WshNetwork Object
Next we will look at the WshNetwork object. For the network administrator, this is the object that will be dealt with the most.
The WshNetwork object contains a number of properties and methods that you can use to work with network resources. Some are included in the following list, but you can find a complete listing in Appendix A, "WSH Reference."
ComputerNameUse this property to return a string value with the name of the computer.
UserDomainThis property is a string value that returns the name of the domain that the user is currently logged in to.
UsernameThis property will return the user's username.
The methods that are contained within this object deal with mapping network drives and _connecting and working with network printers.
Listing 3.9 gives an example of using the WshNetwork object and some of the properties and methods that you can use with it. Once again, for a full reference of the properties and methods available, see Appendix A.
Listing 3.9 A Script File Showing the Use of Some Properties and Methods of the WshNetwork Object
'********************************************************************* ' Network.vbs ' This script file demonstrates the use of the Network object ' of Wscript. It enumerates the network drives and printers ' and displays that information along with the Domain name, ' username and computer name. ' ' Author: Gerry O'Brien ' Date: April 7, 2001 ' Platform: Windows 2000/XP ' Language: VBScript '********************************************************************** Set WshNetwork = WScript.CreateObject("WScript.Network") Set objDrives = WshNetwork.EnumNetworkDrives Set objPrinters = WshNetwork.EnumPrinterConnections WScript.Echo "User Domain is: " & WshNetwork.UserDomain WScript.Echo "Computer Name is: " & WshNetwork.ComputerName WScript.Echo "User Name is: " & WshNetwork.UserName WScript.Echo "Network drive mappings on computer: " For i = 0 to objDrives.Count - 1 Step 2 WScript.Echo "Drive " & objDrives.Item(i) & " = " & objDrives.Item(i+1) Next WScript.Echo "Network printer mappings on computer: " For i = 0 to objPrinters.Count - 1 Step 2 WScript.Echo "Port " & objPrinters.Item(i) & " = " & objPrinters.Item(i+1) Next //********************************************************************* // Network.js // This script file demonstrates the use of the Network object // of Wscript. It enumerates the network drives and printers // and displays that information along with the Domain name, // username and computer name. // // Author: Gerry O'Brien // Date: April 7, 2001 // Platform: Windows 2000/XP // Language: JScript //********************************************************************* var WshNetwork = WScript.CreateObject("WScript.Network"); var Drives = WshNetwork.EnumNetworkDrives(); var Printers = WshNetwork.EnumPrinterConnections(); WScript.Echo("User Domain is: " + WshNetwork.UserDomain); WScript.Echo("Computer Name is: " + WshNetwork.ComputerName); WScript.Echo("User Name is: " + WshNetwork.UserName); WScript.Echo("Network drive mappings on computer: "); for(i=0; i<Drives.Count(); i+=2){ WScript.Echo("Drive " + Drives.Item(i) + " = " + Drives.Item(i+1)); } WScript.Echo("Network printer mappings on computer: "); for(i=0; i<Printers.Count(); i+=2){ WScript.Echo("Port " + Printers.Item(i) + " = " + Printers.Item(i+1)); }
When you run either of these scripts, you are presented with message boxes displaying the user's domain, computer name, and username, as well as the network drive mappings.
The WshNetwork object also enables you to add network printer mappings and to create mapped drives. An example of creating network drive mappings and adding a network printer is shown in Listing 3.10.
Listing 3.10 Using the WshNetwork Object to Map Network Drives and Network Printers
'********************************************************************* ' MapNetDriveandPrinter.vbs ' This script file demonstrates the use of the Network object ' of Wscript. It maps two network drives on the local computer ' that are shared on the server named celeron500 and ensures that ' the connections persist. It also maps a network printer connection ' ' Author: Gerry O'Brien ' Date: April 7, 2001 ' Platform: Windows 2000/XP ' Language: VBScript '********************************************************************* Dim net Set net = CreateObject("Wscript.Network") net.MapNetworkDrive "S:", "\\celeron500\C", _ "True","gobrien","dianne" net.MapNetworkDrive "X:", "\\celeron500\D", _ "True", "gobrien", "dianne" net.AddWindowsPrinterConnection ("\\celeron500\Epson") //********************************************************************* // MapNetDriveandPrinter.js // This script file demonstrates the use of the Network object // of Wscript. It maps two network drives on the local computer // that are shared on the server named celeron500 and ensures that // the connections persist. It also maps a network printer connection // // Author: Gerry O'Brien // Date: April 7, 2001 // Platform: Windows 2000/XP // Language: Jscript //********************************************************************* var net; net = new ActiveXObject("WScript.Network"); net.MapNetworkDrive("S:","\\\\celeron500\\C", _ "True","gobrien","dianne"); net.MapNetworkDrive("X:","\\\\celeron500\\D", _ "True","gobrien","dianne"); net.AddWindowsPrinterConnection ("\\\\celeron500\\EPSON");
Running either one of these scripts will add two mapped network drives to the computer the script is run on, as well as map a network printer. This can be seen in Figures 3.10 and 3.11. In Figure 3.10, note the two network drives titled C on 'celeron500' (S:) and D on 'celeron500' (X:) in the tree view on the left of Explorer.
Figure 3.10 The two mapped network drives added to the Windows Explorer window.
Figure 3.11 The network printer that was added after running the script.
This is just a small sample of the tasks that are available with the WshNetwork object. We will see some more as we progress through the book, and you can also find out the full usage of the available properties and methods in Appendix A.
Using the WshShell Object
The last parent object on the WSH object model is the WshShell object. This object contains some child objects, as can be seen in the object model diagram shown in Figure 3.7. We will start by taking a look at the WshShell object itself.
The main purpose of this object is to provide access to the native shell in Windows. The _primary uses for this object are to run programs on the local computer, create shortcuts, _manipulate the registry, access system folders, and look at environment variables. Let's look at each task here.
The first thing we will do with the WshShell object is create a shortcut on the desktop for Microsoft Word and give it an icon. Listing 3.11 shows the code for this.
Listing 3.11 Using WshShell to Create a Shortcut to Microsoft Word on the Desktop
'********************************************************************* ' shortcut.vbs ' This script file demonstrates the use of the Shell object ' of Wscript. It creates a shortcut on the Desktop for Microsoft Word ' and indicates the icon to use as well as the path to the executable ' file to run the application ' ' Author: Gerry O'Brien ' Date: April 7, 2001 ' Platform: Windows 2000/XP ' Language: VBScript '********************************************************************* set WshShell = WScript.CreateObject("Wscript.Shell") strDesktop = WshShell.SpecialFolders("Desktop") set oShellLink = WshShell.CreateShortcut(strDesktop & _ _"\Microsoft Word.lnk") oShellLink.IconLocation = "d:\Program Files\Microsoft _Office\Office\winword.exe ,0" oShellLink.TargetPath = "d:\Program Files\Microsoft _ _Office\Office\winword.exe" oShellLink.Save //********************************************************************* // shortcut.js // This script file demonstrates the use of the Shell object // of Wscript. It creates a shortcut on the Desktop for Microsoft Word // and indicates the icon to use as well as the path to the executable // file to run the application // // Author: Gerry O'Brien // Date: April 7, 2001 // Platform: Windows 2000/XP // Language: Jscript //********************************************************************* var WshShell = WScript.CreateObject("Wscript.Shell"); strDesktop = WshShell.SpecialFolders("Desktop"); var oShellLink = WshShell.CreateShortcut(strDesktop + _ _"\\Microsoft Word.lnk"); oShellLink.IconLocation = "d:\\Program Files\\Microsoft _Office\\Office\\winword.exe ,1"; oShellLink.TargetPath = "d:\\Program Files\\Microsoft _\ _Office\\Office\\winword.exe "; oShellLink.Save();
This script has some interesting parts to it. Let's look at it one line at a time. First of all, we create an object WshShell to be used in the code. Then we declare a variable strDesktop to hold a reference to the Desktop as a special folder.
Next, we create the shortcut on the desktop by using the variable we just created, and give it the name Microsoft Word. The extension .lnk is used to indicate a shortcut style icon.
Following this we provide a location for the icon that will be used for the shortcut. The icon is located within the Winword executable file. This file contains many icons, and we choose to use the first icon in the available list by using the 0 at the end of the string. You can consider the icon list in the executable to be an array that is 0-based. This means that it is an array that starts at 0 for the first item. You could change this number to other values to see the other available icons.
The next line tells the shortcut where to go for the executable file that it is linked to, and the last line saves the shortcut. This is necessary to have the shortcut show up on the desktop or wherever you have chosen to place it.
Sometimes you might want to start a program automatically, or some users might even want a program to open when they log on. A co-worker at one of my jobs insisted on having her word processor, spreadsheet, and e-mail applications start automatically for her each time she turned on her computer. You could do this in the Startup folder for Windows, true, but we will take a look at how you can perform this using a script file in the event that you want to include this in a login script. The simple script file is shown in Listing 3.12.
Listing 3.12 A Script That Will Open Microsoft Word in a New Window
set WshShell = WScript.CreateObject("Wscript.Shell") WshShell.Run ("Winword.exe")
Another use for the WshShell object is to work with the command prompt. Listing 3.13 shows a simple use of the WshShell object to open the command prompt window, change to the C: drive, issue a make directory command, and change to that new directory.
Listing 3.13 A Script That Uses the Command Prompt cmd.exe to Create a Directory and Switch to It
Dim objShell Set objShell = WScript.CreateObject ("WScript.shell") objShell.run "cmd /K CD C:\ & mkdir \newdirectory & cd newdirectory" Set oShell = Nothing
Taking a look at this script, you can see we create the WshShell object at the top, and then using the run method, we manipulate the command prompt. The /K switch in the command prompt syntax is used to tell Windows to keep the window open after we have executed the command. We will show some of the command-line switches here that you might want to use. For a complete list, see the Windows online help files.
Note
These switches are used when calling the cmd.exe program from within a script, or from the run command found on the Start button menu in Windows.
/CThis switch will cause cmd.exe to execute the command given and then shut down the window.
/KUsing this switch will execute the command, but it will leave the command prompt window open for you to continue working with it.
/QThis switch will turn echo off (the command does not display when executing).
So far, we have taken a look at how to create shortcuts and open an application with the WshShell object. We also mentioned the fact that you can manipulate the registry with this object as well.
Working with Registry Methods and the WshShell Object
In almost any book or instruction that deals with registry manipulation, there are warnings of doom and gloom associated with modifying entries in the registry. This book will not be an exception, as the warnings have merit. Any changes to areas of the registry that you are not sure of are best left alone as they can render the computer unbootable or inoperable.
Having given you that message, however, the methods available for working with the registry are listed here:
RegWriteprovides the capability to write a registry value to an existing key, create a new key, or modify an existing value/name pair
RegReadused to read the value of a key or value/name pair from the registry
RegDeleteused to delete a key from the registry or a value from a key
We will look at the RegWrite method here; you'll find an example of using RegWrite, RegDelete, and RegRead in a code sample a little later in this section. The syntax for RegWrite is as follows:
object.RegWrite (strName, varValue, [,strType])
The arguments are described here:
strNameused to indicate the key name, value name, or value that will be added
varValueused to create a new key or to name a value that you will add to an _existing key
strTypean optional string argument used to indicate the data type for the value (described later)
When you are indicating key names, you must use the abbreviated form. These abbreviated forms are used for all three registry methods and are displayed in Table 3.2.
Table 3.2 Registry Key Abbreviations to Be Used with the Registry Access Methods
Root Key Name |
Abbreviation |
HKEY_CURRENT_USER |
HKCU |
HKEY_LOCAL_MACHINE |
HKLM |
HKEY_CLASSES_ROOT |
HKCR |
HKEY_USERS |
HKEY_USERS |
HKEY_CURRENT_CONFIG |
HKEY_CURRENT_CONFIG |
I can't offer an explanation as to why the HKEY_USERS and CURRENT_CONFIG keys are not _abbreviated.
Table 3.3 shows the available string types that you can use to specify data types for values.
Table 3.3 The Available Data Types for the Values to Be Used in the Optional strType Argument
String Type |
Description |
Data Type |
REG_SZ |
String |
String |
REG_DWORD |
Number |
Integer |
REG_BINARY |
VBArray of Integers |
Binary |
REG_EXPAND_SZ |
Expandable String |
String |
Each string type is described here to help those not familiar with the registry and its data types:
REG_SZThis value is a fixed-length text string. The values are most commonly short strings but they can be Boolean values as well. The SZ on the end stands for String/Zero. This means that it is a zero (zero byte) terminated string. This is how Windows knows the string has ended.
REG_DWORDDWORD stands for Double Word. A Word is a 16-bit value; hence, the DWORD is 32 bits long (two 16-bit Words). This is the most common string type found in the registry and is used to store information for device drivers, Boolean values, and other information. The registry editor normally displays these values in HEX format.
REG_BINARYThis string type is used mostly for storing hardware-specific information. It is stored in raw binary format. You can edit this information in the registry editor in either binary or HEX format.
REG_EXPAND_SZYou will find this string type used to store variables used by an _application or the operating system such as %systemroot% or %windir%.
Let's take a look at adding some keys and values to the registry. These values are completely benign and will cause no ill effects to your system. Use the code shown in Listing 3.14 to see an example of how to add, read, and delete registry values.
Listing 3.14 VBScript and JScript Files That Show How to Write, Read, and Delete Registry Values
'********************************************************************* ' registry.vbs ' This script creates registry keys and values for those keys. ' It also reads the newly created values back in message boxes. ' Uncomment the WshShell.RegDelete lines to delete the entries made ' ' Author: Gerry O'Brien ' Date: April 7, 2001 ' Platform: Windows 2000/XP ' Language: VBScript '********************************************************************** ' Create an object to hold a reference to the Wscript.Shell object Dim objShell Set objShell = WScript.CreateObject("WScript.Shell") ' Create some registry keys and values using RegWrite with objShell .RegWrite "HKCU\Software\GKComputerConsulting\Software\", _ _1, "REG_BINARY" .RegWrite "HKCU\Software\GKComputerConsulting\Hardware\", _ _1, "REG_BINARY" .RegWrite "HKCU\Software\GKComputerConsulting _\Software\ProductName", "ScriptExample", "REG_SZ" .RegWrite "HKCU\Software\GKComputerConsulting _ _\Hardware\Mouse", "USBModel", "REG_SZ" ' Read the values back that were created and display in a message box .RegRead ("HKCU\Software\GKComputerConsulting\Software\") .RegRead ("HKCU\Software\GKComputerConsulting\Hardware\") Wscript.Echo .RegRead ("HKCU\Software\GKComputerConsulting _\Software\ProductName") WScript.Echo .RegRead ("HKCU\Software\GKComputerConsulting _\Hardware\Mouse") End With ' Uncomment these lines to delete the previously created keys and values ' With objShell '.RegDelete "HKCU\Software\GKComputerConsulting _ \Software\ProductName" '.RegDelete "HKCU\Software\GKComputerConsulting _ \Hardware\Mouse" '.RegDelete "HKCU\Software\GKComputerConsulting _\Software\" '.RegDelete "HKCU\Software\GKComputerConsulting _\Hardware\" '.RegDelete "HKCU\Software\GKComputerConsulting\" 'End With //********************************************************************* // registry.js // This script creates registry keys and values for those keys. // It also reads the newly created values back in message boxes. // Uncomment the WshShell.RegDelete lines to delete the entries made // // Author: Gerry O'Brien // Date: April 7, 2001 // Platform: Windows 2000/XP // Language: Jscript //********************************************************************* // Create an object to hold a reference to the Wscript.Shell object var objShell = WScript.CreateObject("WScript.Shell"); // Create some registry keys and values using RegWrite objShell.RegWrite ("HKCU\\Software\\GKComputerConsulting _\\Software\\", 1, "REG_BINARY"); objShell.RegWrite ("HKCU\\Software\\GKComputerConsulting _\\Hardware\\", 1, "REG_BINARY"); objShell.RegWrite ("HKCU\\Software\\GKComputerConsulting _\\Software\\ProductName", "ScriptExample", "REG_SZ"); objShell.RegWrite ("HKCU\\Software\\GKComputerConsulting _\\Hardware\\Mouse", "USBModel", "REG_SZ"); // Read the values back that were created and display in a message box var objKey1 = objShell.RegRead ("HKCU\\Software\\GKComputerConsulting _ _\\Software\\"); var objKey2 = objShell.RegRead ("HKCU\\Software\\GKComputerConsulting _\\Hardware\\"); WScript.Echo (objShell.RegRead ("HKCU\\Software\\GKComputerConsulting _\\Software\\ProductName")); WScript.Echo (objShell.RegRead ("HKCU\\Software\\GKComputerConsulting _\\Hardware\\Mouse")); // Uncomment these lines to delete the previously created keys and values //objShell.RegDelete ("HKCU\\Software\\GKComputerConsulting _\\Software\\ProductName"); //objShell.RegDelete ("HKCU\\Software\\GKComputerConsulting _\\Hardware\\Mouse"); //objShell.RegDelete ("HKCU\\Software\\GKComputerConsulting\\Software\\"); //objShell.RegDelete ("HKCU\\Software\\GKComputerConsulting\\Hardware\\"); //objShell.RegDelete ("HKCU\\Software\\GKComputerConsulting\\");
Both of these scripts perform the same task. They create some keys under the HKEY_CURRENT_USER key and add some values to them. The scripts then read the values _back. If you uncomment the sections indicated in the code listing following the line that reads // Uncomment these lines to delete the previously created keys, the scripts will also delete the created values and keys.
Note
Notice that in order to delete the keys and values, you must delete the values first and then the keys.
Using the WshUrlShortcut Object
We took a look at the capability of the Shell object to create shortcuts earlier. Another child object of the WshShell object is the WshUrlShortcut object. This object permits you to create a shortcut to a URL or Internet resource. The usage is identical to the code found in Listing 3.11. All you have to do is add these lines to the code to create the URL shortcut:
Dim objLink set objLink = WshShell.CreateShortcut(strDesktop & "\Sams _ Publishing.url") objLink.TargetPath = "http://www.samspublishing.com" objLink.Save
This will create a link on the desktop to the Sams Publishing Web site.
Earlier, I made mention of the fact that you can read environment variables with WSH. The WshEnvironment child object of the WshShell object allows you to do this.
Using the WshEnvironment Object
The WshEnvironment object actually contains a collection of variables returned from the WshShell's Environment property. (For more information on the Environment property, check Appendix A, "WSH Reference.")
For an example of how to use the Environment property with the WshShell and the WshEnvironment object, look at the code in Listing 3.15.
Listing 3.15 VBScript and JScript Files Demonstrating the Use of Environment Variables and Displaying Them in Message Boxes
'********************************************************************* ' environment.vbs ' This script file demonstrates the use of the Environment property ' of Wscript.Shell. It displays some environment variables in some ' message boxes that are returned by the Environment Property. ' The variables are returned from the Process and System categories ' ' Author: Gerry O'Brien ' Date: April 7, 2001 ' Platform: Windows 2000/XP ' Language: VBScript '********************************************************************* Set objShell = WScript.CreateObject ("WScript.Shell") Set objProEnv = objShell.Environment ("Process") WScript.Echo "Windows Directory " & objProEnv("windir") WScript.Echo "System Path " & objProEnv("path") ' This set of environment variables will be empty for Windows 9x/ME OS. Set objSysEnv = objShell.Environment("System") WScript.Echo ("Operating System " & objSysEnv("OS")) WScript.Echo ("Temp Directory " & objSysEnv("TEMP")) WScript.Echo ("Extensions in Path " & objSysEnv("PATHEXT")) If objSysEnv("NUMBER_OF_PROCESSORS") = 1 Then WScript.Echo "Your system has 1 Processor" Else WScript.Echo "Your system has " & _objSysEnv("NUMBER_OF_PROCESSORS") & " Processors" End If //********************************************************************* // environment.js // This script file demonstrates the use of the Environment property // of Wscript.Shell. It displays some environment variables in some // message boxes that are returned by the Environment Property. // The variables are returned from the Process and System categories // // Author: Gerry O'Brien // Date: April 7, 2001 // Platform: Windows 2000/XP // Language: Jscript //********************************************************************* var objShell = WScript.CreateObject("WScript.Shell"); var objProEnv = objShell.Environment("Process"); WScript.Echo ("Windows Directory " + objProEnv("windir")); WScript.Echo ("System Path " + objProEnv("path")); // This set of environment variables will be empty for Windows 9x/ME OS. var objSysEnv = objShell.Environment("System"); WScript.Echo ("Operating System " + objSysEnv("OS")); WScript.Echo ("Temp Directory " + objSysEnv("TEMP")); WScript.Echo ("Extensions in Path " + objSysEnv("PATHEXT")); WScript.Echo ("Your system contains " + _objSysEnv("NUMBER_OF_PROCESSORS") + " processor(s)");
Note the use of a decision structure at the end of the VBScript file that determines the number of processors returned by the variable and displays a different message based on that value. This is a prime example of one of the added benefits that scripting has over batch files. Decision structures permit you to run certain pieces of code based on conditions you specify.
The WshSpecialFolders Object
The next object in the model is the WshSpecialFolders object. This object is used to return the Windows Special Folders collection for your use. Examples of Special Folders are the Desktop, Start Menu, and Personal Documents. For an example of using the Special Folders, see the code in Listing 3.11. This code uses the Desktop Special Folder to hold the shortcut that we create. For more information, refer to Appendix A.
The WshScriptExec Object
The final object in the model is WshScriptExec. This object is used to return status information about a script that is running or has been executed with the Exec method. The most common use is to provide a variable in which to store the status information, as shown in this line of JScript code:
var ScriptStatus = WshShell.Exec("%comspec% _ /c scriptname.js");
After you have the status assigned to the variable, you can display it with the Echo method _if you want.