Wednesday, December 9, 2015

Powershell find missing associated PTR resource records in DNS Server

Earlier I written article on "Powershell add A resource records in DNS Domain oneliner", now this post completely focuses on adding PTR record in DNS using powershell, PTR record is opposite of A record, PTR record resolves IPAddress to hostname. You will find them in reverse lookup zone. (Make sure you have added correct subnet reverse lookup zone before adding PTR record).
Alike my earlier article I will be using below 2 cmdlets to add PTR and to know information.
Get-DnsServerResourceRecord
Add-DnsServerResourceRecordPtr

I have provided instructions Powershell add A resource records in DNS Domain oneliner how to open Powershell and add DNS module, Here in this article I am executing all commands on DNS Server it self, Open Powershell (Run as Administrator). 

And fire up below oneliner command
Get-DnsServerResourceRecord -ZoneName "vcloud.lab" -ComputerName WIN2K12AD001 -RRType Ptr

This is the break down of command. 
Get-DnsServerResourceRecord = This is the CMDLET used to retrive all the records. 
-ZoneName "vcloud.lab" = -ZoneName is parameter and vcloud.lab is my domain and zonename in DNS Server. 
-ComputerName WIN2K12AD001 = This is my DNS Server. 
-RRType Ptr = I retriving perticularly PTR record only.
Below is the screenshot of the command execution it is showing blank because I dont have any PTR record.
Next I will Add new PTR resource record. 
Add-DnsServerResourceRecordPtr -Name 100 -ZoneName 33.168.192.in-addr.arpa -ComputerName WIN2K12AD001 -PtrDomainName Test.vcloud.lab -AllowUpdateAny

In above command -name will be the last octet of IP Address., put FQDN in -PtrDomainName parameter.
This is all it, I tried to create PTR record for existing A record (Update associated pointer (PTR) Record), but didnt find much, even online either. if any one know the solution he is welcome. Here I have created small script which shows what PTR records not exist or missing against A resource record.

Replace the information in the orange marked as per your environment.
 #Replace below information as per your infrastructure  
 $DnsServer = "WIN2K12AD001"  
 $ForwardLookupZone = "vCloud.lab"  
 $ReverseLookupZone = "33.168.192.in-addr.arpa"  
 ################################  
 # http://kunaludapi.blogspot.com   
 # Version: 1  
 # Created and Tested on   
 #   Windows Server 2012 R2  
 #   PowerShell 4  
 ###############################  
 $DNSAresources = Get-DnsServerResourceRecord -ZoneName $ForwardLookupZone -RRType A -ComputerName $DnsServer | Where-Object {$_.Hostname -ne "@" -and $_.Hostname -ne "DomainDnsZones" -and $_.Hostname -ne "ForestDnsZones"}  
 foreach ($DnsA in $DNSAresources) {  
   $DNSPtr = ($DNSA.RecordData.IPv4Address.IPAddressToString -split "\.")[3]  
   $DNSPtrRecord = Get-DnsServerResourceRecord -ZoneName $ReverseLookupZone -Name $DNSPtr -RRType Ptr -ErrorAction SilentlyContinue  
   if ($DNSPtrRecord -eq $null) {  
     $DNSPtrRecord = New-Object psobject  
     $DNSPtrRecord | Add-Member -Name Hostname -MemberType NoteProperty -Value "NotExist"  
     $record = New-Object psobject  
     $record | Add-Member -Name PtrDomainName -MemberType NoteProperty -Value "NotExist"  
     $DNSPtrRecord | Add-Member -Name RecordData -MemberType NoteProperty -Value $record  
   }#if  
   else {  
     $PTRHostname = "{0}.{1}." -f $DnsA.HostName, $ForwardLookupZone  
     $DNSPtrRecord = $DNSPtrRecord | Where-Object {$_.RecordData.PtrDomainName -Match $PTRHostname}  
     if ($DNSPtrRecord -eq $null) {  
       $DNSPtrRecord = New-Object psobject  
       $DNSPtrRecord | Add-Member -Name Hostname -MemberType NoteProperty -Value "NotExist"  
       $record = New-Object psobject  
       $record | Add-Member -Name PtrDomainName -MemberType NoteProperty -Value "NotExist"  
       $DNSPtrRecord | Add-Member -Name RecordData -MemberType NoteProperty -Value $record  
     }#$if  
   }#else  
   $Obj = New-Object psobject  
   $Obj | Add-Member -Name Hostname -MemberType NoteProperty -Value $DnsA.Hostname  
   $Obj | Add-Member -Name IP -MemberType NoteProperty -Value $DnsA.Recorddata.IPv4Address.IPAddressToString  
   $Obj | Add-Member -Name DNSPtrRecord -MemberType NoteProperty -Value $DNSPtrRecord.Hostname  
   $Obj | Add-Member -Name PTRHostName -MemberType NoteProperty -Value $DNSPtrRecord.RecordData.PtrDomainName  
   $Obj   
 }#foreach  

I have executed this script on DNS server, if you are using RSAT tool, you can use it remotely, fill up the information, copy paste code in text file, rename extension to .ps1
and execute it using .\filename.ps1 (Make sure powershell execution policy is set to bypass or remotesigned) 
You can check my earlier Powershell articles on how to execute powershell script. once script is executed you will see result something like below, also you can export this list to csv and view it in excel.

Post a Comment