Answered by:
Dump DNS data to CSV

Question
-
Hi,
I want to get the results of the following into a csv file, but when I pipe the output to convertto-csv, the output changes
Get-DnsServerResourceRecord -ComputerName $DNSServer -ZoneName $ZoneName | Where-Object {$_.RecordType -eq "CNAME" -or $_.RecordType -eq "A" }
Returns Hostname, RecordType, timestamp, TimeToLive, and RecordData where RecordData is either an IP address or a FQDN.
When I pipe the output to a csv by adding | ConvertTo-Csv the RecordData changes to the object type, DnsServerResourceRecordCName, or DnsServerResourceRecordA and the specific IP address and FQDN data is lost.
How can I preserve the IP address and FQDN data and write it to the csv file so that I can get the same output in the file as is displayed on the screen?
Thursday, November 9, 2017 8:29 PM
Answers
-
Get-DnsServerResourceRecord -ComputerName $DNSServer -ZoneName $ZoneName |
Where-Object {$_.RecordType -eq "CNAME" -or $_.RecordType -eq "A" } |
Select -expand RecordDataWhat do you see?
\_(ツ)_/
- Marked as answer by jrv Monday, May 6, 2019 5:44 PM
Friday, November 10, 2017 2:56 PM
All replies
-
It should be fairly easy since you have it working already. you just need to pick what value you want to pass onto your csv file. I would just use Select command and Export-CSV command like below
<pre>
Get-DnsServerResourceRecord -ComputerName $DNSServer -ZoneName $ZoneName | Where-Object {$_.RecordType -eq "CNAME" -or $_.RecordType -eq "A" } | Select Hostname, RecordType, timestamp, TimeToLive, RecordData | Export-CSV C:\temp\OutputFile.csv -notype
</pre>
So. Try that command. It should work.
- Edited by Naw Thursday, November 9, 2017 10:19 PM
- Proposed as answer by Albert LingMicrosoft contingent staff Friday, November 10, 2017 6:49 AM
Thursday, November 9, 2017 10:13 PM -
You need to use a calculated select statement to extract the underlying value in the object.
Search for how to use calculated properties.
\_(ツ)_/
Thursday, November 9, 2017 10:29 PM -
Get-DnsServerResourceRecord -ComputerName $DNSServer -ZoneName $ZoneName |
Where-Object {$_.RecordType -eq "CNAME" -or $_.RecordType -eq "A" } |
Select -expand RecordDataWhat do you see?
\_(ツ)_/
- Marked as answer by jrv Monday, May 6, 2019 5:44 PM
Friday, November 10, 2017 2:56 PM -
jrv, I can do this but don't know how to do it using an operator like -or
This works for the FQDN
Get-DnsServerResourceRecord -ComputerName $DNSServer -ZoneName $ZoneName | Where-Object {$_.RecordType -eq "CNAME" } | Select-Object HostName, RecordType, @{N="RecordData"; E={$_.RecordData.HostNameAlias}}
And this works for the IPv4 data
Get-DnsServerResourceRecord -ComputerName $DNSServer -ZoneName $ZoneName | Where-Object {$_.RecordType -eq "A" } | Select-Object HostName, RecordType, @{Name="RecordData"; Expression={$_.RecordData.IPv4Address[0]}}
The original output is using either of the two expressions to fill the same column. I've tried a few variants on the expression but have not gotten it to work yet.
Friday, November 10, 2017 3:12 PM -
Hi Naw, I've tried that but the results as the same as when I don't explicitly select the data. 'RecordData' returns the type name w/o the IP or FQDNFriday, November 10, 2017 3:14 PM
-
You have to alter the return based on the record type.
\_(ツ)_/
Friday, November 10, 2017 3:30 PM -
Try something like this and see what you can get out of it and perhaps use dotted notation to extract the relevant data.
$record = Get-DnsServerResourceRecord -zonename "myzone.com" $record.RecordData | get-member
Friday, November 10, 2017 4:46 PM -
Get-DnsServerResourceRecord -ZoneName "zone.com" | select HostName,RecordType,@{n='IP';E={$_.RecordData.IPV4Address}},@{n='CName';E={$_.RecordData.HostNameAlias}} | Export-Csv "C:\temp\zone.csv"
This should be the correctest way. :)
@{n='IP';E={$_.RecordData.IPV4Address}} @{n='CName';E={$_.RecordData.HostNameAlias}}
The "n=" is simply naming the column of the property in the csv, the "E=" is the expression of the item properties.
Monday, May 6, 2019 5:40 PM