Skip to content

Instantly share code, notes, and snippets.

@blinds52
Forked from magnetikonline/README.md
Created April 15, 2017 18:51
Show Gist options
  • Save blinds52/dae56aa61a26a9bc6c3625cf4275c069 to your computer and use it in GitHub Desktop.
Save blinds52/dae56aa61a26a9bc6c3625cf4275c069 to your computer and use it in GitHub Desktop.

Microsoft DNS server list all records

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

Usage

$ ./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)"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment