When you start working with the Windows Scripting Host and VBS scripting, you must understand the concept of objects and methods. Previously, I discussed how to instantiate, or create, an instance of the FileSystemObject object and use it to manipulate the Windows file system. Now, I’m going to discuss two other objects that are important in network admin scripting, WshShell and WshNetwork. We’ll use these two objects to create shortcuts, write to the registry, and map network drives.

Object recap
Objects, which can be thought of as tools, have what we call methods—certain things the tools can do. In addition, objects have properties. Just as the name implies, properties are characteristics of objects and, like methods, can be used to good advantage in VBS.

Use the WshShell object to create a shortcut
To create the Shell object, we use the CreateObject method of WScript:
Dim objShell
Set objShell = WScript.CreateObject(“WScript.Shell”)

Once the Shell object has been created, it includes the properties and methods shown inTable A.

Table A
Properties Methods
CurrentDirectory AppActivate
Environment CreateShortcut
SpecialFolders Exec
WshShell object

Using the Shell object and some of these properties and methods, we are going to create a VBS script that will create a shortcut to Notepad on the user’s desktop. First, notice that one of the properties is called SpecialFolders. This property requires an argument telling it which special folder to use, such as MyDocuments, Favorites, or StartMenu. For our script, we’ll use Desktop.

Second, notice that one of the methods for WshShell is CreateShortcut. If you use that method, you end up with an object called WshShortcut. Its properties are shown in Table B.

Table B
WshShortcut object

Where do these properties come from? If you right-click on a shortcut and select Properties from the context menu, you’ll see something similar to Figure A.

Figure A

Items in the Properties dialog box correspond to WshShortcut properties. For instance, the Target text box corresponds to the TargetPath property in the WshShortcut object. The Run drop-down list lets you select either Normal Window, Run Minimized, or Run Maximized. These options are part of the WindowStyle property in WshShortcut. The only property we are required to specify is TargetPath.

Now let’s look at how we can create a shortcut with VBS. First, we have to remember that shortcuts to files or folders (as opposed to URLs) have the extension .lnk. We will need to specify that we want to link to C:\WINNT\notepad.exe and store that link (shortcut) in the user’s Desktop folder. Here is what we will do:

  1. 1.      Instantiate the WshShell object.
  2. 2.      Use WshShell to create a shortcut.
  3. 3.      Specify where the link is located (using the TargetPath property of WshShortcut).
  4. 4.      Save the shortcut.

Listing Ashows how this script will look.

Note the various uses of parentheses and quotation marks in this script. Note also that in creating the shortcut, we specified the target folder using its variable name and concatenated the link name using an ampersand (&). In specifying the target path, we used the environment variable %windir%, which is normally C:\WINNT. The entire path must be enclosed in quotes.

Use the WshShell object to work with the registry
As we saw in Table A, the Shell object contains the following methods:

  • ·        RegWrite
  • ·        RegRead
  • ·        RegDelete

We can use these methods to manipulate the registry of a machine with a VBS script—that is, read, write, and delete registry keys and values. The syntax for this is quite simple. Let’s say that in the HKEY_CURRENT_USER hive (HKCU), we want to create a key with the name “AnewKey” and assign to it the value of “NewValue.” We would first instantiate the Shell object and then use the RegWrite method as follows:
Dim objShell
Set objShell = WScript.CreateObject(“WScript.Shell”)
ObShell.RegWrite “HKCU\AnewKey”, “NewValue”

The same key and value can be removed using the RegDelete method.

Use the WshNetwork object to map a network drive
Table Cshows the properties and method of the WshNetwork object. We’ll use the MapNetworkDrive method in this example.

Table C
Properties Methods
ComputerName AddWindowsPrinterConnection
UserDomain AddPrinterConnection
UserName EnumNetworkDrives
WshNetwork object

Let’s say that we want to assign the network share \\Server1\Admin to drive G:. We first instantiate the network object and then use the MapNetworkDrive method as follows:
Dim objNetwork
Set objNetwork = WScript.CreateObject(“WScript.Network”)
ObjNetwork.MapNetworkDrive “G:”, “\\Server1\Admin”

The sky’s the limit
This article has introduced the capabilities available for network administration using VBS scripting. Using the FileSystemObject, Shell, and Network objects and their associated properties and methods, you can automate a great many tasks to ease the burden of routine work.

To learn more about VBS and how you can use the objects, methods, and properties to your advantage, check out Microsoft’s site for specifics such as the VBS User’s Guide and Language Reference and references on the individual objects Shell, Network, and FileSystemObject.