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 = "192.168.1.10,192.168.1.11";
@newWINSservers = ("192.168.1.12","192.168.1.13");
open SERVERS,"servers.txt";
$newWINSserversString = join " ",@newWINSservers;
while(<SERVERS>){
 chomp($server=$_);
 print uc($server)."\n";
if($nics=$Registry->{"//$server/LMachine/System/CurrentControlSet/Services/Tcpip/Parameters/Interfaces"}){
  @nicGuids= $nics->SubKeyNames;
  foreach $nicGuid (@nicGuids){
#update DNS addresses -----------------------
   if($currentDNSservers=$Registry->{"//$server/LMachine/System/CurrentControlSet/Services/Tcpip/Parameters/Interfaces/$nicGuid//NameServer"}){
    print "   DNS:  $currentDNSservers\n";
    if($currentDNSservers ne $newDNSservers){
     $Registry->{"//$server/LMachine/System/CurrentControlSet/Services/Tcpip/Parameters/Interfaces/$nicGuid//NameServer"}=$newDNSservers;
    }
   }
   # -------------------------------------------
#check to see if this server is a WINS server.  If it is, leave it alone.
   $comp=Win32::OLE->GetObject("WinNT://$server");
   $svc=$comp->GetObject("Service","WINS");
   if($svc->{status} == 4){
    print "  This is a WINS server\n";
   }else{
#update WINS addresses ----------------------
    if($currentWINSservers=$Registry->{"//$server/LMachine/System/CurrentControlSet/Services/NetBT/Parameters/Interfaces/Tcpip_$nicGuid//NameServerList"}){
     if($currentWINSservers ne "\0"){
      print "  WINS:  $currentWINSservers\n";
      if($currentWINSservers ne $newWINSserversString){
       $Registry->{"//$server/LMachine/System/CurrentControlSet/Services/NetBT/Parameters/Interfaces/Tcpip_$nicGuid//NameServerList"}=[[@newWINSservers],REG_MULTI_SZ];
      }
     }
    }
    # -------------------------------------------
}
  }
 }else{
  print "  Could not connect to $server registry\n";
 }
}

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...