Not getting IPs for a DNS Zone dump

I have this code:

$dnszones = Get-DnsServerZone | Select ZoneName

ForEach ($zone in $dnszones) { $data = New-Object System.Object

$ZoneName = $Zone.ZoneName
$data = Get-DnsServerResourceRecord $ZoneName
$data | Add-Member -MemberType NoteProperty -Name "ZoneName" -Value $ZoneName

$data += $data

$data | Select ZoneName, HostName, RecordType -expandproperty recorddata | Export-Csv -NoTypeInformation .\DNSRecords.csv -Append

}

…but it’s not outputting the record data that I expect (the IP addresses)

Can someone please suggest a fix?

Thanks

There is nothing wrong with what you have here. It does output the IPA of the DNS registered resources, when the export is not used. It’s the Export that is the problem child using that expand directly as you are. You have to grab the items from there, inline, you want then combine as needed.

Here is an example. Maybe not the best or most elegant way, but it gets the records and shows the IPAs you say you are missing on your effort.

Clear-Host
$dnszones = Get-DnsServerZone `
| Select ZoneName

ForEach ($zone in $dnszones)
{ 
    $data = New-Object System.Object

    $ZoneName = $Zone.ZoneName
    $data = Get-DnsServerResourceRecord $ZoneName
    $data `
    | Add-Member -MemberType NoteProperty -Name "ZoneName" -Value $ZoneName

    $data += $data

    $data `
    | Select * -expandproperty recorddata  -ErrorAction SilentlyContinue `
    | Select ZoneName, HostName, RecordType, IPv4Address `
    | Export-Csv -NoTypeInformation .\DNSRecords.csv -Append
}
Import-Csv -Path .\DNSRecords.csv | Format-Table -AutoSize

Thanks again. I was expecting the “recordData” to hold the IP address but you added “IPv4Address” to my select and that indeed then output my IPs.

I want the values for the CNAMES. When I do:

$data | gm
I see these contenders:

DistinguishedName         Property     string DistinguishedName {get;}                                                                      
HostName                  Property     string HostName {get;}                                                                               
PSComputerName            Property     string PSComputerName {get;}                                                                         
RecordClass               Property     string RecordClass {get;}                                                                            
RecordData                Property     CimInstance#Instance RecordData {get;set;}                                                           
RecordType                Property     string RecordType {get;} 

I don’t see a property that has this value. How would I figure this out?

As for…

I was expecting the "recordData" to hold the IP address but you added "IPv4Address" to my select and that indeed then output my IPs.
It does, you just can't get at it in the export. It's has to be exposed before the export, hence the two selects.

The reason for this is the unlike A/AAAA records. CNAMS’s, are just alias pointers, with no PTR reference to them. So, more effort is requires to discover the name / IPA of the associated A / AAAA record.

You have to use another method to get that. For example, iterate the returned data and use nslookup on the CNAME directly, since nslookup will return the alias of the CNAME and the associated server.

nslookup SomeCNAMEHere

I found it as Property “HostNameAlias” all good, thanks.

No worries. Good to know you found it.