Remove stale DNS records

In today article I want to show you script which how in automatic way remove stale DNS records.

In big environments there is often situation that DNS contains stale records which are causing mess.
Script which I prepared is scanning dns zone defined at the beginning for records which timestamp is older than specific number of days (NumberOfDaysBack variable).
Once all are gathered, script remove stale DNS records.

Script:

$ZoneName = "zone name"
$NumberOfDaysBack = 30
$DateInThePast = (Get-Date).AddDays(-$NumberOfDaysBack)
# Provide record types separated by comma, varibale can be leave empty
$RecordTypes = "Record Type"
$RecordTypes = $RecordTypes.Split(',')

if([string]::IsNullOrEmpty($RecordTypes))
{
    $i = 0
    $RecordsArray = (Get-DnsServerResourceRecord -ZoneName $ZoneName |Where-Object {($_.Timestamp -lt $DateInThePast) -and ($_.Timestamp -ne $null)})[0]
    $RecordsArray | Export-Csv -Path $env:TEMP\$(Get-Date -Format dd_MM_yyyy)_$ZoneName'_AllRecords'.csv -NoTypeInformation
    $RecordsCounter = $RecordsArray.Count
    $Acceptance = Read-Host -Prompt "Do you want to remove $RecordsCounter records from $ZoneName zone? (Y/N)"
    if(($Acceptance -eq 'y') -or ($Acceptance -eq 'yes'))
    {
        foreach($Record in $RecordsArray)
        {
            $i++
            Write-Progress -Activity "Removing stale records from $ZoneName zone" -Status "Percent complete" -PercentComplete (($i/$RecordsCounter)*100)
            Try
            {
                Remove-DnsServerResourceRecord -InputObject $Record -ZoneName $ZoneName -Force
            }
            Catch
            {
                $_.Exception.Message
            }
        }
    }
    else
    {
        Write-Host Removing of stale records from $ZoneName zone has been skipped by user 
    }
}
else
{
    foreach($RecordType in $RecordTypes)
    {
        $i = 0
        $RecordsArray = Get-DnsServerResourceRecord -ZoneName $ZoneName -RRType $RecordType |Where-Object {($_.Timestamp -lt $DateInThePast) -and ($_.Timestamp -ne $null)}
        $RecordsArray | Export-Csv -Path $env:TEMP\$(Get-Date -Format dd_MM_yyyy)_$ZoneName'_RecordType_'$RecordType.csv -NoTypeInformation
        $RecordsCounter = $RecordsArray.Count
        $Acceptance = Read-Host -Prompt "Do you want to remove $RecordsCounter $RecordType records from $ZoneName zone? (Y/N)"
        if(($Acceptance -eq 'y') -or ($Acceptance -eq 'yes'))
        {
            foreach($Record in $RecordsArray)
            {
                $i++
                Write-Progress -Activity "Removing stale $RecordType records from $ZoneName zone" -Status "Percent complete" -PercentComplete (($i/$RecordsCounter)*100)
                Try
                {
                    Remove-DnsServerResourceRecord -InputObject $Record -ZoneName $ZoneName -Force
                }
                Catch
                {
                    $_.Exception.Message
                }
            }
        }
        else
        {
            Write-Host Removing of stale $RecordType records from $ZoneName zone has been skipped by user 
        }
        
    }
}

Write-Host Script has been completed
Write-Host All remove records have been exported to CSV files under path $env:TEMP

NOTE:
Please be sure that all variables are defined correctly. Otherwise you can cause damage in 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.