Subscribe to Windows IT Pro

 

Get Newsletters

  • Get the Latest News
  • Product Updates
  • Helpful Tricks
  • Productivity Tips

Subscribe Now!

February 25, 2003 12:00 AM

Editing the Registry

Use the WshShell object to make and record registry changes
Windows IT Pro
InstantDoc ID #37940
Rating: (0)
Downloads
37940.zip

You probably use Control Panel or other tools for most server-tuning tasks, but some recommended fixes require you to edit the registry by hand. Manually editing the registry can easily mess up your OS to the point of no return, so if a registry modification is important enough to make by hand, it's important enough to make consistently on all applicable computers. You probably also want to document the registry edit and its purpose, as well as confirm the changes.

If you've ever manually edited the registry on more than a few computers, you probably know one way to make changes without navigating through the HKEY maze more than once. You can save the edited key as a .reg file, then import that file to all the computers that need the same fix. However, that approach doesn't log the change and supports only a limited amount of granularity (you can import a key, but you can't import one edited value). A better approach is to use VBScript to write your change to the registry and record changes in the computer's Application log so that you—and others—can tell what edits you've made and why. You can modify the same script to read the registry and thus confirm your changes.

A Good Example
You can edit any part of the registry through VBScript, but for an example, let's suppose that you're editing the registry on your application servers so that a particular application will refer to usernames instead of computer names. The edit is application-specific, so you must be sure to edit the correct application's subkey. The subkey's final edited value is the sum of two hexadecimal numbers (rather than something easy, such as 0 or 1), so you must be sure to enter the right number on all application servers. The edit's purpose isn't obvious, so you'll want a record explaining the edit.

To accomplish these goals, you need a script that performs several tasks. The script must create a WshShell object (which represents the Windows Shell and thus gives you programmatic access to the registry-editing tools and Event Viewer), write the change to the registry, and record the change in the Application log.

Writing the Change
I discuss WshNetwork, a Windows Script Host (WSH) 2.0 object representing network-accessible printers and drives, in "Connecting to Printers," August 2002, http://www.winnetmag.com, InstantDoc ID 25652, and "Connecting Users to Network Resources," June 2002, http://www.winnetmag.com, InstantDoc ID 24893. WshShell, another WSH object, deals with certain Windows Shell­related objects, including the registry editor and Event Viewer.

WshShell supports several methods for reading or editing the registry. Generally, using the registry-editing methods is simple as long as you're exact; putting even an extra space in the string will return an error. For this example, you're going to work with the RegRead method, which reads a subkey or value, and the RegWrite method, which writes a subkey or value. These methods use a simple syntax:

WshShell.RegRead strName

and

WshShell.RegWrite strName, varValue, [strtype]

Related Content:

ARTICLE TOOLS

Comments
  • David Figueroa
    9 years ago
    Aug 27, 2003

    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.

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

White Papers

Get your Windows 7 deployment off to the right start by implementing PC lockdown. A locked-down environment is easier and cheaper to support since users are less likely to make unnecessary changes to the core system configuration - read more here!

Essential Guides

Is your iSCSI "lossy"? The reality is that most off-the-shelf Ethernet hardware deployed for iSCSI can lose packets, resulting in slow performance or application downtime. Learn how to assess your current iSCSI infrastructure and engineer an advanced iSCSI SAN infrastructure.

Web Seminars

What's the best way to keep your network safe from malware? In this web seminar, security expert Greg Shields suggests an alternative method to the traditional blacklisting approach that is common with anti-virus and anti-malware solutions.

eLearning Series

We bring the experts direct to you to share their real-world perspective and expertise. During each event, three sessions stream in real time, so you can learn, ask questions, and get solutions.
Upcoming event: Getting the Most with Exchange 2010 with Paul Robichaux

Subscribe to Windows IT Pro!

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.