Change DNS and WINS settings using Perl

A Windows Script to Change DNS and WINS Addresses on Network Cards Across Your Network:  Suppose you're in the midst of an Active Directory migration or whatever, and you want to retire some old DNS servers or WINS servers, and you want to move everyone to new DNS and WINS servers that are at different IP addresses. Most organizations use DHCP to configure their clients, so you can just change the DHCP server or scope options that set the DNS and WINS pointers, but all their servers are hard-coded. So how do you reconfigure a few hundred servers to point to new DNS and WINS addresses? Perl it out man. Perl it out.

This script takes a list of server names from a text file, and remotely sets their DNS and WINS pointers. It does this through remote registry reads and writes. The DNS pointers are in a REG_SZ string value, separated by commas. The WINS pointers are in a REG_MULTI_SZ multi-value string value (which is a null terminated list). So the script shows how to write both types of values.

One other thing this script does, it checks to see if the target server is a WINS server in which case it does not change the WINS pointers. This is because a WINS server should always point to itself.

DNS and WINS pointers are adapter specific, meaning that if your machine has multiple network interface cards (nics), more than likely only one nic will have WINS and DNS pointers assigned. The others likely will not have any. So, the script enumerates through the list of nics on the machine, searching for any that have DNS or WINS pointers, and fixes them, leaving the others blank as they were.

use Win32::OLE;
use Win32::TieRegistry(Delimiter=>"/",ArrayValues=>0);
$newDNSservers = ",";
@newWINSservers = ("","");
open SERVERS,"servers.txt";
$newWINSserversString = join " ",@newWINSservers;
 print uc($server)."\n";
  @nicGuids= $nics->SubKeyNames;
  foreach $nicGuid (@nicGuids){
#update DNS addresses -----------------------
    print "   DNS:  $currentDNSservers\n";
    if($currentDNSservers ne $newDNSservers){
   # -------------------------------------------
#check to see if this server is a WINS server.  If it is, leave it alone.
   if($svc->{status} == 4){
    print "  This is a WINS server\n";
#update WINS addresses ----------------------
     if($currentWINSservers ne "\0"){
      print "  WINS:  $currentWINSservers\n";
      if($currentWINSservers ne $newWINSserversString){
    # -------------------------------------------
  print "  Could not connect to $server registry\n";


Post a Comment

Related Posts Plugin for WordPress, Blogger...