Thursday, July 9, 2015

powershell to list old dns records and then delete them

#list_old_records
$records = Get-DnsServerResourceRecord -ZoneName example.com
$records = $records | ? Timestamp -ne $null
$records = $records | ? Timestamp -lt 3/01/2015
Write-Output $records

copy the output to a file then regex below in notepad++ to just get the hosts

\s*\w\s*\d*\/\d*\/+\d{4}\s.*\r\n

The use the file as an import for the following powershell

#delete_old_records
$DNSServer = "dns1.example.com"
$DNSZone = "example.com"
$InputFile = "hosts.txt"
import-module DnsServer
$recordnames = Get-Content $InputFile

# Now we loop through the file to delete the records
ForEach ($recordname in $recordnames) {
Remove-DnsServerResourceRecord -Name $recordname -RRType A -ZoneName $DNSZone -ComputerName $DNSServer -Force
}

                               

No comments:

Post a Comment