Subscribe to Windows IT Pro
January 01, 1998 12:00 AM

Modify the Registry with Perl

Windows IT Pro
InstantDoc ID #3456
Rating: (1)
Bob Wells and Toby Everett present a Perl script that automates WINS changes

[Editor's Note: Scripting Solutions is about using scripts to build solutions to specific business problems. This column doesn't teach you how to program with scripting languages, but how to use scripts to provide quick, easy-to-implement tools you can use right away.]

"Automating NT Administration with Scripts" (November 1997) explained Windows NT scripting languages and how they offer a more flexible and powerful approach to solving NT administration problems. This article continues down that path by walking through a useful script to access and manipulate the Windows NT Registry. The script, WINSer.pl (shown in Listing 1, page 206), demonstrates how you can use Practical Extraction and Report Language (Perl) for Win32 to automate Windows Internet Naming Service (WINS) client IP address changes in an enterprise network. Although the script targets WINS addresses, you can easily modify it to accommodate almost any batch Registry change.

Perl for Win32
ActiveWare Internet (now ActiveState Tool) developed Perl for Win32 to fill the need for a strong scripting language on the NT and Windows 95 platforms. Perl for Win32 is a port of most of the functionality available in the version of Perl for UNIX environments. The WINSer.pl script demonstrates some of Perl's key concepts and capabilities. For a quick introduction to Perl fundamentals, see the sidebar, "Perl Basics," page 208. If you decide Perl can be of use to you, buy a good book about Perl and explore some online Perl resources (for information about Perl books and Web sites, see the online sidebar, "Perl Resources," at http://www.winntmag.com).

Before you can use WINSer.pl, you need to install Perl for Win32. Simply download the distribution file from the ActiveState Tool Web site (http://www.activestate.com) and run it. We used Build 306 (i.e., pw32i306.exe) to build the script in this article. After extracting all the files, the Perl for Win32 installation process invokes an install script to update path and Registry information. When installation is complete, you're ready to start writing and using Perl scripts. One word of caution: If you've installed the version of Perl that comes with Microsoft Windows NT Server Resource Kit or Microsoft Windows NT Workstation Resource Kit, you'll encounter version incompatibility problems. The resource kit version of Perl is an old version that we do not recommend you use. You'll want to remove the resource kit version or at least make sure that c:\Perl\bin (or the like) appears before the resource kit directory entry in your path statement.

The Sample Script
WINSer.pl is a robust tool that lets you update WINS client IP addresses on local or remote servers and workstations. To run WINSer.pl, you simply enter a command sequence at the command prompt. The number of parameters you include in the command line depends on whether you want to update the local machine, a remote machine, or multiple machines. In every case, the first two parameters are the primary and secondary WINS server IP addresses. To update the local machine, enter

c:\> perl winser.pl <xxx.xxx.xxx.xxx>  <xxx.xxx.xxx.xxx>

To update a remote machine, enter

c:\> perl winser.pl <xxx.xxx.xxx.xxx>  <xxx.xxx.xxx.xxx> remote <hostname>

To update multiple machines, enter

c:\> perl winser.pl <xxx.xxx.xxx.xxx> <xxx.xxx.xxx.xxx> file <filename>

where filename is the name of a text file that contains the names (one per line) of the machines you want to update.

WINSer.pl uses several functions in the Perl for Win32 Registry library, registry.pll, for accessing registries on remote computers. A friendlier interface for manipulating the Registry is available through the Win32::Registry module. However, this module lacks support for connecting to remote registries. (For descriptions of the functions in registry.pll, see the online table, "Registry.pll Functions," at http://www.winntmag.com.)

One of the more powerful, but less obvious, features of WINSer.pl has to do with the values we're changing—WINS addresses. Anyone who has worked with NT for a while knows that the location of the WINS entries in the Registry depends on the type of network adapter installed in the machine. WINSer.pl gets around this problem by connecting to the deepest common key, HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\NetBT\Adapters, and enumerating its subkeys. For each subkey, WINSer.pl tests the subkey to make sure it is not a Remote Access Service (RAS) entry (i.e., the script checks whether the subkey contains the string NdisWan). If the subkey is not a RAS subkey, WINSer.pl updates its values. Of course, you must restart the machine for the change to take effect. You can add that piece of functionality to the script with a call to

Win32::InitiateSystemShutdown ($machine, $message, $timeout, $forceclose, $reboot)

but you risk annoying logged on users with this automatic reboot.

Related Content:

ARTICLE TOOLS

Comments
  • Anonymous User
    7 years ago
    Feb 15, 2005

    I have tried. this code doesn't work

  • Scott
    9 years ago
    Nov 26, 2003

    This dosnt work. I wish sometimes that they would flog people that place stuff in news groups that DONT WORK! or the poster should have enough sense to TEST THERE SCRIPT OUT BEFORE writing about it.

    Have a great day!

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

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