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:
#<span class="short_text" id="result_box" lang="en"><span>The name of the computer with primary DNS</span></span>
$computer = "srvhost01"
#<span class="short_text" id="result_box" lang="en"><span>Ip address of the computer with primary DNS</span></span>
$main_ip = "192.168.133.190"
$zones = Get-DnsServerZone -computername $computer
#<span class="short_text" id="result_box" lang="en"><span>secondary DNS IP addresses</span></span>
$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
#<span class="short_text" id="result_box" lang="en"><span>Adding permission to read zones to primary DNS</span></span>
Set-DnsServerPrimaryZone -ComputerName $computer -Name $zones[$k].ZoneName -Notify NotifyServers -NotifyServers $servers -SecureSecondaries TransferToSecureServers -SecondaryServers $servers -PassThru
#<span class="short_text" id="result_box" lang="en"><span>Actually copying</span></span>
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
}
}
