DOWNLOAD THE CODE:
Download the Code 37940.zip

The RegRead method reads the key or value that you specify with the strName argument. Each method accesses registry keys according to the keys' full pathname, so you must use the familiar predefined key names as part of the strName argument:

  • HKEY_CURRENT_USER (or HKCU)
  • HKEY_CLASSES_ROOT (or HKCR)
  • HKEY_LOCAL_MACHINE (or HKLM)
  • HKEY_USERS
  • HKEY_CURRENT_CONFIG

The RegWrite method writes to the registry the subkey or value that you specify with the varValue argument. If strName ends with a backslash (\), RegWrite writes varValue to the registry as a subkey. Otherwise, RegWrite writes varValue to the registry as a value. You can use the optional strtype argument to define the value's type: REG_SZ or REG_EXPAND_SZ writes string values, REG_DWORD writes a 32-bit integer value, and REG_BINARY writes a 32-bit binary value.

Now that you have a basic understanding of the tools, you can build a registry-editing script, which Listing 1 shows. To customize this script for your environment, simply substitute the appropriate subkey and value.

Recording and Confirming the Change
The script that Listing 1 shows makes an edit, but with this script, the only ways to prove that you made a change are to run the application (to show the expected behavior) or to browse through the registry. If neither approach appeals to you, you can add some code to the script to record and describe the change in the Application log.

To record a change in this manner, employ the RegRead method with another method: LogEvent. LogEvent is almost as simple as RegRead but takes as arguments both a string—which you can use to explain the change—and a code that identifies the type of event that took place. These types, which Table 1 lists, are familiar to anyone who uses the Event Viewer. LogEvent uses the following syntax:

WshShell.LogEvent eventcode, strmessage [strtarget]

The eventcode argument specifies the type of event to record, the strmessage argument specifies the message that appears in the Application log, and the optional strtarget argument supplies the name of the computer on which to record the changes. (You can keep a centralized record—in which case you can use WshNetwork's ComputerName property to identify the computer for which you made the change—or you can maintain the record on the local server.) The final version of the script, which Listing 2 shows, is similar to the script that Listing 1 shows but includes a new variable and the LogEvent method.

Sometimes you need to confirm and record, rather than change, a server's registry settings. For example, suppose someone edits a server's registry to change the port on which Windows 2000 Server Terminal Services listens (port 3389 by default). Anyone who attempts to connect to that server without specifying the new port will receive a vague and unhelpful error message stating only that the client couldn't connect to the terminal server. To troubleshoot this problem, you can use a short script, such as the one that Listing 3 shows, to read the registry and determine on which port Terminal Services is listening. This script uses WshShell's RegRead method (instead of RegWrite) to get the current value of HKEY_LOCAL_MACHINE\CurrentControlSet\Control\Terminal Server\WinStations\RDP-TCP\PortNumber, assign the value to a variable, then write that value to the Application log with explanatory text.

Quick, Simple, and Consistent
Any time you need to make the same registry edit to many servers (and record the change) or want to determine current registry settings without going hunting, the WshShell RegRead, RegWrite, and LogEvent methods can save you a lot of time and trouble. And you can probably think of several other VBScript capabilities that might enhance these registry-editing tools. How about using the InputBox function (which I describe in "Connecting to Printers") to prompt the person running the script for a specific value? Or why not use Select Case (which I also describe in "Connecting to Printers") so that one script can apply different registry edits to different groups of servers? These useful tools offer multiple options.

End of Article

Prev. page     1 [2]     next page -->



You must log on before posting a comment.

If you don't have a username & password, please register now.

Reader Comments

The code listed in Listing 1 will not work. You cannot use () marks around a method unless it is used as part of an assignment.

oWshShell.RegWrite ("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Terminal Server\Compatibility\Applications\AppName\Flags", 18)

This Should read:

oWshShell.RegWrite "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Terminal Server\Compatibility\Applications\AppName\Flags", 18

Otherwise it will error out with a syntax error.

David Figueroa

 
 

ADS BY GOOGLE