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


January 1998

Modify the Registry with Perl


RSS
Subscribe to Windows IT Pro | See More Registry Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!
SideBar    Perl Basics, Sidebar for Scripting Solutions, Scripting Solutions Table

Download the Code Here

 See corrections to this article

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.

   Previous  [1]  2  Next 


Corrections to this Article:

  • The code for the Perl program, WINSer.pl, featured in Bob Wells' article, "Modify the Registry with Perl," contained errors.The corrected code is available from "Windows NT Magazine's" Web site at http://www.winntmag.com.
Top Viewed ArticlesView all articles
Battery Life Issues Almost Certainly Not Windows 7's Fault

While Microsoft is still investigating a notebook battery life issue that was supposedly caused by Windows 7, some interesting trends have emerged. ...

Confirmed: Battery Life Issues Not Windows 7's Fault

Microsoft on Monday issued a lengthy statement about the recent Windows 7 battery controversy, echoing my assessment from earlier in the day, but backing it up with hard, cold evidence. ...

Microsoft Warns of Windows Version Expirations

Microsoft warned that this year will see three out-of-date Windows versions slip into retirement. ...


Related Articles Automating NT Administration with Scripts

Task Automation Whitepapers Three Ways to Prevent Insider Risk within Your SCCM-Managed Environment

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!

Task Automation eBooks Spam Fighting and Email Security for the 21st Century

A Guide to Windows Certification and Public Keys

Keeping Your Business Safe from Attack: Patch Management

Related Task Automation 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.
 © 2010 Penton Media, Inc. Terms of Use | Privacy Statement