Not getting IPs for a DNS Zone dump

Welcome Forums General PowerShell Q&A Not getting IPs for a DNS Zone dump

This topic contains 7 replies, has 2 voices, and was last updated by

 
Participant
4 days, 16 hours ago.

  • Author
    Posts
  • #113540

    Participant
    Points: 0
    Rank: Member

    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

  • #113546

    Participant
    Points: 10
    Rank: Member

    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
    
  • #113615

    Participant
    Points: 0
    Rank: Member

    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.

  • #114151

    Participant
    Points: 0
    Rank: Member

    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?

  • #114159

    Participant
    Points: 10
    Rank: Member

    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.

  • #114163

    Participant
    Points: 10
    Rank: Member

    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

  • #114166

    Participant
    Points: 0
    Rank: Member

    I found it as Property "HostNameAlias" all good, thanks.

  • #114171

    Participant
    Points: 10
    Rank: Member

    No worries. Good to know you found it.

You must be logged in to reply to this topic.