retrieve DNS records help

Matt 101 Reputation points
2022-05-26T16:55:51.377+00:00

Hi all,

We have many reverse lookup zones in our AD integrated zone of our domain mydomain.local.
How to retrieve all PTR records for each reverse lookup zones through script?

Unfortunately I don't have much knowledge of how to create Powershell script, so I hope someone has an example
for me similar to this that I can reuse.

Thank you!

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,628 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Rich Matheisen 47,886 Reputation points
    2022-05-26T18:15:10.97+00:00

    Like this: Get-DnsServerResourceRecord -ZoneName "reverse-lookup-zone-name"


  2. Rich Matheisen 47,886 Reputation points
    2022-05-28T14:54:29.73+00:00

    When dealing with PTR records, the HOST is that part of the IP address that uniquely identifies the PTR record within the zone. The name in the PTR record (while it's usually a DNS name) doesn't necessarily refer to an A (or any other type of DNS record). There's nothin in DNS that states, for instance, that an A or AAAA record has to have an associated PTR record, or that a PTR record has to represent the DNS name of any other record in your (or any) DNS servers forward lookup zones.

    Given that, this will attempt to find a PTR record given the data in the CSV:

    # CSV has these columns: Host, ZoneName, Data
        Import-CSV YOUR-CSV-NAME-GOES-HERE |
            ForEach-Object{
                Get-DNSServerResourceRecord -ComputerName YOUR-DNS-SERVER-NAME -name $_.Host -zonename $_.ZoneName -RRType PTR
            }
    

    Note that there's no error handling, and that I have no idea what you want to do with the information in the PTR if one is found.


  3. Rich Matheisen 47,886 Reputation points
    2022-06-10T02:37:52.323+00:00

    Give this a try -- NOTE: it must be "Run as administrator". Also, it works only with AD-integrated DNS zones:

    $Zone = "1.168.192.in-addr.arpa"  
    $DnsServer = (Get-ADDomain).ReplicaDirectoryServers[0]  
    $DnsRecordQueryParams = @{  
        'Computername' = $DnsServer  
        'Class'        = 'MicrosoftDNS_PTRType'  
        'Namespace'    = 'root\MicrosoftDNS'  
        'Filter'       = "ContainerName = '$Zone' AND OwnerName <> '$Zone'"     # AND Timestamp <> 0 <=== add this if you want only dynamic records  
    }  
      
    Get-WmiObject @DnsRecordQueryParams |   
        ForEach-Object {  
            $parts = $_.Textrepresentation -split " "  
            $reverse = $parts[0] -replace '\.in-addr\.arpa', ""  
            $ipparts = $reverse -split "\."                      # IPv4 ONLY!!!!!  
            $ip = $ipparts[(($ipparts.count - 1)..0)]  
          
            [PSCustomObject]@{  
                'DnsServerName' = $DnsServer  
                'Zone' = $_.DomainName  
                'Host' = $ipparts[0]  
                'Name' = $_.RecordData  
                'Type' = if ($_.timestamp -gt 0) { 'Dynamic' } else { 'Static' }  
                'IP'   = ($ip -join ".")  
            }  
        }  
    
    
    
    
      
      
      
      
      
      
      
      
      
      
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.