You are here

Managing zones on a Microsoft Windows 2012 DNS server using PowerShell scripts


Hello. There was a task to migrate DNS servers. It was necessary to migrate secondary and primary DNS servers. The following are examples of powershell scripts that allow you to copy all zones from the primary DNS server to secondary, copy all the zones from the primary server to the primary server, and delete all zones on the server.

Copying zones:

#The name of the computer with primary DNS
$computer = "srvhost01"
#Ip address of the computer with primary DNS
$main_ip = "192.168.133.190"
$zones = Get-DnsServerZone -computername $computer
#secondary DNS IP addresses
$servers = "192.168.133.196","192.168.133.187"
for ($k=0; $k -lt $zones.Length; $k++) {
    if ((!$zones[$k].IsAutoCreated) -and ($zones[$k].ZoneName -ne "TrustAnchors")) {
        $zf=$zones[$k].ZoneName +".dns"
        $zones[$k].ZoneName
        #Adding permission to read zones to primary DNS
        Set-DnsServerPrimaryZone -ComputerName $computer -Name $zones[$k].ZoneName -Notify NotifyServers -NotifyServers $servers -SecureSecondaries TransferToSecureServers -SecondaryServers $servers -PassThru
        #Actually copying
        Add-DnsServerSecondaryZone -name $zones[$k].ZoneName -zonefile $zf -MasterServers $main_ip
        }
    }

Copy zones to the primary server.

$computer = "srvhost01"
$main_ip = "192.168.133.190"
$zones = Get-DnsServerZone -computername $computer
$servers = "192.168.133.196","192.168.133.187","192.168.133.181"
$serversnew = "192.168.133.196","192.168.133.187"
for ($k=0; $k -lt $zones.Length; $k++) {
    if ((!$zones[$k].IsAutoCreated) -and ($zones[$k].ZoneName -ne "TrustAnchors")) {
        $zf=$zones[$k].ZoneName +".dns"
        $zones[$k].ZoneName
        Set-DnsServerPrimaryZone -ComputerName $computer -Name $zones[$k].ZoneName -Notify NotifyServers -NotifyServers $servers -SecureSecondaries TransferToSecureServers -SecondaryServers $servers -PassThru
        Add-DnsServerSecondaryZone -name $zones[$k].ZoneName -zonefile $zf -MasterServers $main_ip
        ConvertTo-DnsServerPrimaryZone -name $zones[$k].ZoneName -zonefile $zf -force
        Set-DnsServerPrimaryZone -Name $zones[$k].ZoneName -Notify NotifyServers -NotifyServers $serversnew -SecureSecondaries TransferToSecureServers -SecondaryServers $serversnew -PassThru
        }
    }

 

And removing zones from the server:

$zones = Get-DnsServerZone -ComputerName srvvportalnew
for ($k=0; $k -lt $zones.Length; $k++) {
    if ((!$zones[$k].IsAutoCreated) -and ($zones[$k].ZoneName -ne "TrustAnchors")) {
        $zf=$zones[$k].ZoneName +".dns"
        $zones[$k].ZoneName
        Remove-DnsServerZone -name $zones[$k].ZoneName -force
        }
    }

 

0 0

Share the article with your friends in social networks, maybe it will be useful to them.


If the article helped you, you can >>thank the author<<