Configure DNS servers (Perl)

This code can be found in Chapter 1 of Windows Server 2003 Networking Recipes

Purchase XP Cookbook or Networking Recipes for only $25 plus shipping! While supplies last.

Find out how to download all of the Perl code from this site.

# From the book "Windows Server 2003 Networking Recipes"

# This code will configure the local computer
#   with two DNS servers: 192.168.1.151 and 192.168.1.152
# ------ SCRIPT CONFIGURATION ------

use Win32::OLE qw(in);

$strComputer = '.';
$arrDNSServers = ['192.168.1.151', '192.168.1.152'];
# --------- END CONFIGURATION ------

$objWMIService = Win32::OLE->GetObject('winmgmts:' . '{impersonationLevel=impersonate}!\\\\' . $strComputer . '\\root\\cimv2');
$Nics = $objWMIService->ExecQuery('SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True');

foreach my $Nic (in $Nics) {
    $intSetDNSServers = $Nic->SetDNSServerSearchOrder($arrDNSServers);
    if ($intSetDNSServers == 0) {
        print " DNS Servers set!\n";
    }
    else {
        print " Error setting DNS server info.\n";
    }
}

This code has been viewed 921 times.

New from the creators of TechTasks.com: StatSheet.com