use Win32::OLE;
$Win32::OLE::Warn = 3;
$Win32::OLE::Warn = 3;
use strict;

my $arrServers;
my ($strUsername, $strPassword);
my $dicDNSConfig;

# Tablica sprawdzanych serwerw DNS
$arrServers = ['dns01.rallencorp.com', 'dns02.rallencorp.com'];

# Konto i haso uytkownika upowanionego do zmiany konfiguracji serwerw DNS
$strUsername = 'admin';
$strPassword = 'net';

# Obiekt sownikowy bdzie przechowywa pary (zoone z kluczy i wartoci) dla
# wszystkich ustawie serwerw DNS, ktre maj zosta sprawdzone i skonfigurowane.
$dicDNSConfig = Win32::OLE->new('Scripting.Dictionary');
$dicDNSConfig->Add('AllowUpdate', 1);
$dicDNSConfig->Add('LooseWildCarding', 1);
$dicDNSConfig->Add('MaxCacheTTL', 900);
$dicDNSConfig->Add('MaxNegativeCacheTTL', 60);
$dicDNSConfig->Add('EventLogLevel', 0);
$dicDNSConfig->Add('StrictFileParsing', 1);
$dicDNSConfig->Add('DisableAutoReverseZones', 1);

my $arrDNSConfigKeys;
$arrDNSConfigKeys = $dicDNSConfig->Keys;

my $objLocator;
$objLocator = Win32::OLE->new('WbemScripting.SWbemLocator');

my ($x, $y, $boolRestart);
for my $x ($[ .. $#$arrServers) {
    $boolRestart = 0;

    print $$arrServers[$x], "\n";

    my ($objDNS, $objDNSServer);
    $objDNS = $objLocator->ConnectServer($$arrServers[$x], 'root\\MicrosoftDNS', $strUsername, $strPassword);
    $objDNSServer = $objDNS->Get('MicrosoftDNS_Server.Name="."');

    for my $y (0 .. $dicDNSConfig->Count - 1) {
        my $strKey;
        $strKey = $$arrDNSConfigKeys[$y];

        print '  Checking ' . $strKey, "\n";
        if ($dicDNSConfig->Item($strKey) ne $objDNSServer->Properties_->Item($strKey)->Value) {
            $objDNSServer->Properties_->Item($strKey)->{Value} = $dicDNSConfig->Item($strKey);
            $objDNSServer->Put_;
            $boolRestart = 1;
            if (Win32::OLE->LastError()) {
                print '    Wystpi bd podczas ustawiania ' . $strKey . ' : ' . ('' . Win32::OLE->LastError()), "\n";
                exit 0;
            }
            else {
                print '    ' . $strKey . ' uaktualniony.', "\n";
            }
        }
    }

    if ($boolRestart) {
        $objDNSServer->StopService();
        if (Win32::OLE->LastError()) {
            print 'Metoda StopService zwrcia bd: ' . ('' . Win32::OLE->LastError()), "\n";
            exit 0;
        }

        $objDNSServer->StartService();
        if (Win32::OLE->LastError()) {
            print 'Metoda StartService zwrcia bd: ' . ('' . Win32::OLE->LastError()), "\n";
            exit 0;
        }
        print "Usuga ponownie uruchomiona.\n";
    }

    print "\n";
}
