UPDATED 6/16/2016 Thanks for the comments!
Here’s a nice quick script to list all DNS records in each zone on the DNS server(includes sub-zones):
From the DNS Server
$Zones = @(Get-DnsServerZone) ForEach ($Zone in $Zones) { Write-Host "`n$($Zone.ZoneName)" -ForegroundColor "Green" $Zone | Get-DnsServerResourceRecord }
From a Remote DNS Server
$DNSServer = "servernameOrIp" $Zones = @(Get-DnsServerZone -ComputerName $DNSServer) ForEach ($Zone in $Zones) { Write-Host "`n$($Zone.ZoneName)" -ForegroundColor "Green" $Zone | Get-DnsServerResourceRecord -ComputerName $DNSServer }
From a Remote DNS Server (Output to Tab Delimited File)
$DNSServer = "servernameOrIp" $Zones = @(Get-DnsServerZone -ComputerName $DNSServer) ForEach ($Zone in $Zones) { Write-Host "`n$($Zone.ZoneName)" -ForegroundColor "Green" $Results = $Zone | Get-DnsServerResourceRecord -ComputerName $DNSServer echo $Results > "$($Zone.ZoneName).txt" }
18 comments
Skip to comment form
Great short script. Thank you for posting it. May I recommend that you name your output file with the extension .CSV since you are exporting CSV data and add the “-ComputerName” parameter to both your Get-DnsServerZone and Get-DnsServerResourceRecord so that it can be run from anywhere not just from the DNS server itself.
Anyway, thank you for your post
Author
Thanks for the input, did a small update based on this
It does not return any IP-addresses to CSV. If you run it without pipeing to anywhere, you can see IP-addressess though. Might be related the properities having sub-properties.
Author
Just did a quick mod that will output the data correctly
Hello great script, but i have some long entry that when exported they appear like this:
x._domainkey.cyberoffres TXT 0 00:05:00 k=rsa; p=DEW22WKEJRNLqXXX222DQ98ASDKKGNADCBi…
Is there a way to export the complete character??
Best regards
For futur generation 🙂 I got my solution here’s what i added
$DNSServer = “servernameOrIp”
$Zones = @(Get-DnsServerZone -ComputerName $DNSServer)
ForEach ($Zone in $Zones) {
Write-Host “`n$($Zone.ZoneName)” -ForegroundColor “Green”
$Results = $Zone | Get-DnsServerResourceRecord -ComputerName $DNSServer | Format-Table -AutoSize -Wrap
echo $Results > “$($Zone.Zo
i have multiple servers but i need to take DNS server details for .xlsx .how can i get it. using power shell script
Author
I would suggest outputting to CSV then you can easily convert to XLSX by opening/saving. If you wanted to automate this, you can use COM objects to do this, but don’t really recommend it (Neither does Microsoft) bc office is designed to work on an interactive desktop not scripted. That being said, here’s a simple example to convert (Note: You must have Excel installed):
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $true
$excel.Workbooks.Open(“data.csv”).SaveAs(“data.xlsx”,51)
$excel.Quit()
Is there any way to get this to sort by timestamp? Or at least put the csv so the timestamp is in a separate field? I didn’t see any Parameters to do this:
Get-DnsServerResourceRecord
[[-Name] ]
[-ComputerName ]
[-ZoneName]
[-Node]
[-ZoneScope ]
[-VirtualizationInstance ]
[-RRType ]
[-CimSession ]
[-ThrottleLimit ]
[-AsJob]
[]
Try this:
GetDnsServerResourceRecord – | Sort TimeStamp
or
GetDnsServerResourceRecord – | Export-Csv
Is there anyway to import from a remote server and save the output to a CSV file?
@ShadowFly: you need to sort the output before writing it down, there’s a separate Sort-Object cmdlet for this, I have added your requirement in the code:
$DNSServer = “servernameOrIp”
$Zones = @(Get-DnsServerZone -ComputerName $DNSServer)
ForEach ($Zone in $Zones) {
Write-Host “`n$($Zone.ZoneName)” -ForegroundColor “Green”
$Results = $Zone | Get-DnsServerResourceRecord -ComputerName $DNSServer | Sort-Object Timestamp -Descending | Format-Table -AutoSize -Wrap
echo $Results > “$($Zone.ZoneName).txt”
}
tks a lot
May I recommend:
# https://github.com/lawson2305/Powershell/blob/master/DNSZoneRecord.ps1
$DNSServer = “SERVER”
$Zones = @(Get-DnsServerZone -computername $DNSServer)
#
# Get all zone information for a Domain Zone
#
$DNS = ForEach ($Zone in $Zones) {
Get-DnsServerResourceRecord -Zonename $zone.ZoneName -ComputerName $DNSServer | Select-Object hostname, recordType, @{n=’ZoneName’;Expression={$zone.ZoneName}},@{n=’Data’;e={
$rr = $_
switch ($rr.RecordType) {
A’ {$rr.RecordData.IPv4Address}
CNAME’ {$rr.RecordData.HostnameAlias}
NS’ {$rr.RecordData.NameServer}
SOA’ {$rr.RecordData.PrimaryServer}
SRV’ {$rr.RecordData.DomainName}
PTR’ {$rr.RecordData.PtrDomainName}
MX’ {$rr.RecordData.MailExchange}
AAAA’ {$rr.RecordData.IPv6Address}
TXT’ {$rr.RecordData.DescriptiveText}
}
}}}
$DNS | Export-Csv -NoTypeInformation -Path dns.csv
Hi Can you please tell me
How to get the DNS entries for about 20 hosts , listed in a zone file, so we can run it against different DNS servers and Then compare results.
I’m late to the party: this only works from a DNS server (Windows running the Windows DNS service), so it won’t run on a workstation, even with RSAT. dnscmd works from a workstation or a server. A sad limitation for PowerShell as we progress into the 21st Century….
Should you only be interested in simple, ip records try this:
#
# Description:
# Outputs all DNS records where a hostname resolves to an IP address
# Output is grouped by DNSzone and written to both console and csv file
# filename is determined from the zonename
#
$strOutputPath = “c:\temp”
md $strOutputPath -force
$DNSServer = read-host -Prompt “Enter DNS Server name or IP”
$Zones = @(Get-DnsServerZone -ComputerName $DNSServer)
ForEach ($Zone in $Zones) {
Write-Host “`n$($Zone.ZoneName)” -ForegroundColor “Green” | ft -AutoSize
$Results = $Zone | Get-DnsServerResourceRecord -ComputerName $DNSServer
$zoneRecords = @()
foreach ($record in $Results) {
$strHostname = $record.HostName
$strIp = $record.Recorddata.IPv4Address.IPAddressToString
if ($strIp) {
$zoneRecord = $(“$strHostname,$strIp”)
Write-Host $zoneRecord
$zoneRecords += $zoneRecord
}
}
if ($zoneRecords) {
$zoneRecords | Sort-Object | Out-File “$strOutputPath\$($Zone.ZoneName).csv”
}
}
i need a script which will provide remote servers dns ip details.