PowerShell script to query a given Microsoft DNS server for all available primary zones and list all records defined within of types:
- A
- AAAA
- CNAME
- MX
- NS
- PTR
- TXT
$ ./dnsserverlistallrecords.ps1 -DNSServerHostname MS_DNS_SERVER_HOSTNAME
| param ( | |
| [parameter(Mandatory=$true)] [string]$DNSServerHostname | |
| ) | |
| Set-StrictMode -Version Latest | |
| function getFullHostname($zoneName,$DNSRecord) { | |
| $hostName = $DNSRecord.HostName | |
| if ($hostName -eq "@") { | |
| return $zoneName | |
| } | |
| return "$($hostName).$($zoneName)" | |
| } | |
| function getDNSRecordValue($DNSRecord) { | |
| $recordData = $DNSRecord.RecordData; | |
| switch ($DNSRecord.RecordType) { | |
| "A" { return $recordData.IPv4Address } | |
| "AAAA" { return $recordData.IPv6Address } | |
| "CNAME" { return $recordData.HostNameAlias } | |
| "MX" { return "$($recordData.MailExchange) [$($recordData.Preference)]" } | |
| "NS" { return $recordData.NameServer } | |
| "PTR" { return $recordData.PtrDomainName } | |
| "TXT" { return $recordData.DescriptiveText } | |
| default { | |
| # skip record type | |
| return $null | |
| } | |
| } | |
| } | |
| # fetch all DNS server zones | |
| Get-DnsServerZone ` | |
| -ComputerName $DNSServerHostname | ForEach-Object { | |
| # skip zones which are not primary | |
| if ($_.ZoneType -ne "Primary") { | |
| return | |
| } | |
| $zoneName = $_.ZoneName | |
| # fetch all records for zone | |
| Get-DnsServerResourceRecord ` | |
| -ComputerName $DNSServerHostname ` | |
| -ZoneName $zoneName | ForEach-Object { | |
| # extract DNS record value based on type, skip record types we don't care for | |
| $recordValue = getDNSRecordValue $_ | |
| if ($recordValue -eq $null) { | |
| return | |
| } | |
| # output hostname, record type, record value | |
| Write-Output "$(getFullHostname $zoneName $_)`t$($_.RecordType)`t$($recordValue)" | |
| } | |
| } |