Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


March 2003

Editing the Registry

Use the WshShell object to make and record registry changes
RSS
Subscribe to Windows IT Pro | See More Registry Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

Download the Code Here

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

   Previous  1  [2]  Next  


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 August 27, 2003


You must be a registered user or online subscriber to comment on this article. Please log on before posting a comment. Are you a new visitor? Register now




Top Viewed ArticlesView all articles
WinInfo Short Takes: Week of November 23, 2009

An often irreverent look at some of the week's other news, including some post-PDC some soul searching, a Google Chrome OS announcement and a Microsoft response, Windows 7 off to a supposedly strong start, the Jonas Brothers and Xbox 360, and so much more ...

Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

2009 Windows IT Pro Editors' Best and Community Choice Awards

Picking a favorite product from an impressive crowd of competitive offerings is never an easy task, and such was the case with our Editors' Best and Community Choice awards this year. ...


Scripting Whitepapers From Development to Production: Streamlining SharePoint Deployment with DocAve Deployment Manager

Related Events Deep Dive into Windows Server 2008 R2 presented by John Savill

Check out our list of Free Email Newsletters!

Scripting eBooks Keeping Your Business Safe from Attack: Encryption and Certificate Services

Best Practices for Managing Linux and UNIX Servers

Building an Effective Reporting System

Related Scripting Resources Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement