Cleaning up old DNS NS records in an Active Directory forest

Cleaning up old DNS NS records in an Active Directory forest,

needed some powershell to help remove all the old DNS server names from various domains that have been removed from the forest over time or demoted / decommissioned.

$dnszones = Get-DnsServerZone
foreach ( $dnz in $dnszones ) { 
    $dnz 
    $z1= $dnz  | Get-DnsServerResourceRecord  -name "@" -rrtype "NS"
    $bad1=@() ; 
    foreach ( $z in $z1 ) {
        # $z.recorddata
        try { 
            $i= Resolve-DnsName $z.RecordData.NameServer -erroraction stop 
            } 
        catch {
            $bad1 += $z.recorddata.nameserver
            "$($z.RecordData.NameServer) error add to bad list " | write-host -ForegroundColor DarkYellow
            }
        }
        foreach ( $b in $bad1 ) {
            "removing $b from $dnz.zonename" | Write-host -ForegroundColor Yellow
            Remove-DNSServerResourceRecord -zonename  $dnz.zonename –Name “@” –RRType NS –RecordData $b  -force
        }
}

About Jeff Turner

Technical director of Nano Tera Network Solutions.
This entry was posted in Powershell, VBS, VBA and other scripting., System Administration. Bookmark the permalink.