Accessing the Windows Registry via Perl

Here's a script that demonstrates how to access the Windows registry with a simple Perl script. This script reads a list of server names from a text file (servers.txt), and for each server in the list, it connects to the server's registry, and retrieves the OS version and the service pack version, then outputs a file (osversions.txt) with that information. It's a good way to figure out how many servers you have left to put the latest service pack on. For an example of how to write to the registry, please have a look at this script: http://www.ITAdminTools.com/2010/07/configure-servers-to-use-tnsnamesora-on.html


use Win32::TieRegistry(Delimiter=>"/",ArrayValues=>0);
%osname=("1381","Windows NT 4.0", "2195","Windows 2000", "3790","Windows 2003", "6002","Windows 2008", "9999","Server Unreachable");
open SERVERS,"servers.txt";
open OUT,">osversions.txt";
while(<SERVERS>){
 chomp($server=$_);
 $server=~s/\s+//g;
 if(!($buildnum = $Registry->{"//$server/LMachine/Software/microsoft/Windows NT/CurrentVersion//CurrentBuildNumber"})){ $buildnum="9999"; }
 if(!($sp = $Registry->{"//$server/LMachine/Software/microsoft/Windows NT/CurrentVersion//CSDVersion"})){ $sp="Service Pack 0"; }
 print"$server \t$osname{$buildnum}\t$sp\n";
 print OUT "$server\t$osname{$buildnum}\t$sp\n";
}
close OUT;

By the way, the current build number is just that, a number. So the script maps these numbers to friendly OS version names (2195 = Windows 2000, 3790=Windows 2003, etc). As new versions of Windows come out, launch regedit, look at the build number, and add it to the list.

There's a lot more to accessing the registry than this script demonstrates. For one thing, we're reading string values. There's other registry data types like dwords, multi's and binaries that require some further demonstration, but I'll get to that in another post.


0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...