How to Get The Real RecordData from DnsServerResourceRecord Using Powershell?

17,782

Solution 1

To select the PtrDomainName property from the DnsServerResourceRecordPtr object, use a calculated property:

... |Select-Object HostName, @{Name='RecordData';Expression={$_.RecordData.PtrDomainName}}

Solution 2

$dnsserver = "yourowndnsserver"
$dnszones = Get-DnsServerZone -ComputerName $dnsserver | Select-Object ZoneName 

ForEach ($zone in $dnszones) {
    $data = New-Object System.Object 
    $ZoneName = $zone.ZoneName 
    $data = Get-DnsServerResourceRecord $ZoneName -ComputerName $dnsserver
    foreach ($registros in $data) {
        $data = $ZoneName
        $data += ","
        $data += $registros.hostname;
        $data += ","
        $data += $RecordType = $registros.recordType;
        $data += ","
        
        if ($RecordType -like "PTR") {
            $data += $registros.RecordData.PtrDomainName
        }
        elseif ($RecordType -like "A") {
            $data += $([system.version]($registros.RecordData.ipv4address.IPAddressToString));
        }
        elseif ($RecordType -like "CNAME") {
            $data += $registros.RecordData.HostNameAlias;
        }
        elseif ($RecordType -like "NS") {
            $data += $registros.RecordData.nameserver;
        }
        elseif ($RecordType -like "MX") {
            $data += $registros.RecordData.MailExchange;
        }
        elseif ($RecordType -like "SOA") {
            $data += $registros.RecordData.PrimaryServer;
        }
        elseif ($RecordType -like "SRV") {
            $data += $registros.RecordData.DomainName;
        }
        $data | out-file -FilePath $env:TEMP\$(Get-Date -Format dd_MM_yyyy)_DNSRecords.csv -Append
    }
}

Solution 3

Yes it's really weird that you can't just call ToString on the DNS record data, it's all formatted using the PowerShell formatters which you can only access with Format-List or Format-Table, rather than just calling $resourceRecord.RecordData.ToString().

I've added more data types than Krzysztof Madej by just hacking out the PowerShell formatters from the XML file, the details are here. http://david-homer.blogspot.com/2020/10/getting-text-representation-of.html

Share:
17,782
Michael J
Author by

Michael J

Updated on June 19, 2022

Comments

  • Michael J
    Michael J almost 2 years

    I'm using PowerShell to extract information from an Active Directory DNS server and I'm having trouble getting to the data I want.

    Specifically, I'm trying to get the names of hosts that belong to a particular subnet, 10.104.128.x.

    When I use the following commands:

    Get-DnsServerResourceRecord -ComputerName AD_DNS_SERVER -ZoneName 104.10.in-addr.arpa -RRType Ptr | Where-Object {$_.HostName -like '*.128'}`

    I get output that looks like this:

    HostName                  RecordType Timestamp            TimeToLive      RecordData                                        
    --------                  ---------- ---------            ----------      ----------                                        
    104.128                   PTR        10/19/2015 3:00:0... 00:15:00        adl5c260a86ba79.XYZ.net.                          
    11.128                    PTR        12/29/2015 6:00:0... 00:15:00        adl3c970e8d7166.XYZ.net.                          
    110.128                   PTR        1/29/2012 11:00:0... 00:15:00        nroxitow7tst.ABC.com.                       
    114.128                   PTR        1/20/2012 7:00:00 AM 00:15:00        adl5c260a86c29e.ABC.com
    

    What I really want are the first column, (HostName), which has the last two octets of the IP; and the fifth column, (RecordData), which has the name of the host the IP is assigned to.

    The hostname is the data I really want/need. And I see it right there!

    So I used the select command to pare down the output in the pipe train. New command looks like this:

    Get-DnsServerResourceRecord -ComputerName AD_DNS_SERVER  -ZoneName 104.10.in-addr.arpa -RRType Ptr | Where-Object {$_.HostName -like '*.128'} | select HostName, RecordData

    But the output looks like this:

    HostName RecordData                
    -------- ----------               
    104.128  DnsServerResourceRecordPtr
    11.128   DnsServerResourceRecordPtr
    110.128  DnsServerResourceRecordPtr
    114.128  DnsServerResourceRecordPtr
    

    Dosen't get me the hostname though. Just the type of object the RecordData is but not the data that the object contains, perhaps?

    I also tried piping the output to CSV and got the same result.

    Then I tried looking at the DnsServerResourceRecord object properties with Get-Member. That showed me the object had a property called PSComputerName. I thought maybe that would have the name of the host but that came up blank when I tried to select it.

    I then Googled around a bit and found a few pages that recommended a few ways to use RecordData.ipv4address to coax the data out of the DnsServerResourceRecordPtr object but I haven't gotten any of them to work yet. Output still prints blanks.

    So my question is: does a reliable method exist for getting the actual hostname from a PTR record?