In this blog post, we detail a PowerShell script designed to export on-prem DNS, all forward lookup zones and associated records, to a dedicated folder for easy analysis and management. Let’s cover the script’s purpose, implementation, and the resulting CSV output.
Script Purpose and Implementation
This script automates the process of retrieving and exporting all forward lookup zones from your DNS servers. It leverages PowerShell to efficiently gather the necessary data and creates a CSV file containing each record’s information, allowing for easy sorting and analysis. The script’s primary goal is to provide a centralized location for your DNS records, simplifying monitoring and troubleshooting.
Zone Retrieval
The script first retrieves all primary forward lookup zones using the Get-DnsServerZone cmdlet, filtering for zones that are not reverse lookups. Here is the reference for the cmdlet. Then, it iterates through each zone, extracting the ZoneName and ZoneType for each record.
# Set output folder
$outputFolder = "C:\temp\DNSExport"
# Create output folder if it doesn’t exist
if (-not (Test-Path $outputFolder)) {
New-Item -Path $outputFolder -ItemType Directory | Out-Null
}
# Get all forward lookup zones
$zones = Get-DnsServerZone | Where-Object { $_.ZoneType -eq 'Primary' -and $_.IsReverseLookupZone -eq $false }
foreach ($zone in $zones) {
$zoneName = $zone.ZoneName
$records = Get-DnsServerResourceRecord -ZoneName $zoneName
$recordData = foreach ($record in $records) {
$ipAddress = ""
if ($record.RecordType -eq "A") {
$ipAddress = $record.RecordData.IPv4Address.IPAddressToString
} elseif ($record.RecordType -eq "AAAA") {
$ipAddress = $record.RecordData.IPv6Address.IPAddressToString
}
[PSCustomObject]@{
HostName = $record.HostName
RecordType = $record.RecordType
RecordClass = $record.RecordClass
TimeToLive = $record.TimeToLive
RecordData = $record.RecordData.ToString()
IPAddress = $ipAddress
}
}
$csvPath = Join-Path $outputFolder "$zoneName.csv"
$recordData | Export-Csv -Path $csvPath -NoTypeInformation -Encoding UTF8
}
Write-Host "DNS records exported to $outputFolder"
Result of export on-prem DNS
The script successfully exports the information of all forward lookup zones to a CSV file named “$zoneName.csv” in the specified directory. This file can be easily opened with any spreadsheet program for further analysis and management of your DNS records.
This post was originally from my blog, referenced below.
Haven't joined Publish0x yet? Join up by using my referral code.
Support work you enjoy with Brave Rewards here.
You can get a 25 PRE token bonus if you use my referral code here. This is to support a decentralized web search engine with presearch.org.
You can earn crypto at Odysee.com, an alternative to YouTube. Use my affiliate link here to watch and earn.
God bless you!
