Remove wrong PTR records from your DNS

Recently I wanted to remove wrong PTR records in one of the customer production environment.
After quick analysis I observed that it is not so easy easy to do manually, so decided to write Powershell script.

What script is doing?
Script gahters all PTR records from Reverse lookup zone provided in $ZoneName variable.
In next step it convert PTR record to standard IP format and checks if hostname record assigned to PTR exist in DNS.
It also checks if IP address for hostname is same as PTR record, if not it remove wrong PTR records.

Script:

#Input data
$ZoneName = "Reverse lookup zone name"
$RecordsPTR = Get-DnsServerResourceRecord -ZoneName $ZoneName -RRType Ptr

# Define arrays, count all pTR records and null loop counters
$PTRRecordsToRemove = @()
$PTRRecordsOK = @()
$PTRCounter = $RecordsPTR.Count
$a,$c = 0

$ZoneFirstOctet = $ZoneName.Split(".")[0]

$PTRCounter
foreach($record in $RecordsPTR)
{
        # Convert to IP address 
        $PTRIP = $ZoneFirstOctet+"."
        $SPlit = $record.HostName.Split(".")
        for($i=1;$i -le 3;$i++)
        {
            $PTRIP +=$SPlit[-$i]+"."
        }
        $PTRIP = $PTRIP.Substring(0,$PTRIP.Length-1)

        Write-Progress -Activity "Processing PTR records" -Status "Percent complete.." -PercentComplete (($a/$PTRCounter) *100)
        $DNSName = $record.RecordData.PtrDomainName
        $DNSName = $DNSName.substring(0,$DNSName.length-1)
        Try
        {
           $lookup = $null
           #Check if for hostname assigned to PTR A record exist
           $lookup = [System.Net.Dns]::GetHostAddresses($DNSName)
           if($lookup.Count -gt 1)
           {
                $lookup = $lookup[1].IPAddressToString
            
           }
           else
           {
                $lookup = $lookup.IPAddressToString
           }
           
        }
        Catch
        {
           # If there is no A record for hostname remove PTR record
           $PTRRecordsToRemove += $record
        }
        
         
        if(![string]::IsNullOrEmpty($lookup))
        {
            
            if($lookup -eq $PTRCounter)
            {
                $PTRRecordsOK += $record
            }
            else
            {
                $PTRRecordsToRemove += $record
            }
        }
    $a++
}

# Removing wrong PTR records
$PTRRecordsToRemoveCounter = $PTRRecordsToRemove.Count
foreach($PTRRemove in $PTRRecordsToRemove)
{
    $c++
    Write-Progress -Activity "Removing incorrect PTR records" -Status "Percent complete" -PercentComplete (($c/$PTRRecordsToRemoveCounter)*100)
    Try
    {
        Remove-DnsServerResourceRecord -InputObject $PTRRemove -ZoneName $ZoneName -Force -ErrorAction Stop
    }
    Catch
    {
        Write-Host Record for PTR $PTRRemove.HostName - $PTRRemove.RecordData.PtrDomainName already removed -ForegroundColor Yellow
    }
}
$PTRRecordsOK | Export-Csv -Path $env:TEMP\$(Get-Date -Format dd_MM_yyyy)_PTRRecordsOK.csv -NoTypeInformation
$PTRRecordsToRemove | Export-Csv -Path $env:TEMP\$(Get-Date -Format dd_MM_yyyy)_PTRRecordsRemoved.csv -NoTypeInformation

Write-Host "----------------------"
Write-Host Report:
Write-Host PTR records OK: ($PTRRecordsOK | Sort-Object -Unique -Property HostName).Count
Write-Host PTR records to Remove: $PTRRecordsToRemove.Count
Write-Host "Report files can be found under $env:TEMP"
Write-Host "----------------------"

Result:

NOTE!!!
Be aware that we don’t take responsibility if script will cause some damage in your environment.

I hope it will be usefull for some of you 🙂
Enjoy!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.