Last active
May 19, 2022 17:14
-
-
Save jimdiroffii/6fa5cc40b193ebb437451d217b105c7c to your computer and use it in GitHub Desktop.
Remove all old DNS server entries from Forward and Reverse Zones in Active Directory DNS using Powershell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Original idea: https://devblogs.microsoft.com/scripting/clean-up-domain-controller-dns-records-with-powershell/ | |
# dnsSrv is only the only the hostname, no domain, of active DNS server, i.e. MYDNSSERVER | |
$dnsSrv = "<active-dns-server-hostname>" | |
# the server we are trying to remove, FQDN is Fully Qualified Domain Name, i.e. MYDNSSERVER.mydomain.local | |
$oldSrvFQDN = "<old-dns-FQDN>" | |
$oldSrvHost = "<old-dns-Hostname>" | |
$oldSrvIp = "<old-dns-ip-address>" | |
# get full list of zones | |
$zones = Get-DnsServerZone -ComputerName $dnsSrv | Select-Object -Property ZoneName | |
# try adjusting the loop for only a couple iterations while testing, replace with $zones.Length | |
for ($i = 0; $i -lt $zones.Length; $i++) { | |
$dnsRecord = Get-DnsServerResourceRecord -ComputerName $dnsSrv -ZoneName $zones[$i].ZoneName | |
$oldRecord = $dnsRecord | | |
Where-Object { | |
$_.RecordData.IPv4Address -eq $oldSrvIp -or | |
$_.RecordData.NameServer -eq $oldSrvFQDN -or | |
$_.RecordData.NameServer -eq $oldSrvHost -or | |
$_.RecordData.DomainName -eq $oldSrvFQDN -or | |
$_.RecordData.DomainName -eq $oldSrvHost | |
} | |
# Replace -WhatIf with -Force when ready to execute | |
$oldRecord | Remove-DnsServerResourceRecord -ComputerName $dnsSrv -ZoneName $zones[$i].ZoneName -WhatIf | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment