Skip to content

Instantly share code, notes, and snippets.

@msoler8785
Last active April 2, 2024 19:25
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save msoler8785/498332c622f93ace02b5d05e47845001 to your computer and use it in GitHub Desktop.
Save msoler8785/498332c622f93ace02b5d05e47845001 to your computer and use it in GitHub Desktop.
Quick PowerShell script to automate PTR Record creation for existing forward lookup zones.
# Creates PTR Records for all A Records in the specified -ZoneName.
# Uses a Class A Subnet for the reverse zone.
$computerName = 'dns-server01';
# Get all the DNS A Records.
$records = Get-DnsServerResourceRecord -ZoneName 'zone.example.com' -RRType A -ComputerName $computerName;
foreach ($record in $records)
{
# The reverse lookup domain name. This is the PTR Response.
$ptrDomain = $record.HostName + '.zone.example.com';
# Reverse the IP Address for the name record.
$name = ($record.RecordData.IPv4Address.ToString() -replace '^(\d+)\.(\d+)\.(\d+).(\d+)$','$4.$3.$2');
# Add the new PTR record.
Add-DnsServerResourceRecordPtr -Name $name -ZoneName '10.in-addr.arpa' -ComputerName $computerName -PtrDomainName $ptrDomain;
}
@SweetAsNZ
Copy link

Fixed up incorrect IP address and made it a bit easier for noobs or those in a hurry.

`# Creates PTR Records for all A Records in the specified -ZoneName.

$ComputerName = (Get-ADDomainController).Name;
$ReverseZone = '112.2.10.in-addr.arpa'
$Domain = '.Contoso.Com'
$Zone = 'Contoso.Com'

Get all the DNS A Records

$Collection = Get-DnsServerResourceRecord -ZoneName $Zone -RRType A -ComputerName $ComputerName |
Where {($.Hostname -ne '@') -and ($.Hostname -ne 'ForestDnsZones') -and ($_.Hostname -ne 'DomainDnsZones') };

foreach ($Record in $Collection)
{
# The Reverse Lookup Domain Name. This is the PTR Response
$PTRDomain = $Record.HostName + $Domain;

# Reverse the IP Address for the name record. ^=Line Beginning, \d=0-9, $=end of line. Replaces the full IP with the last Octet
$Name = ($Record.RecordData.IPv4Address.ToString() -replace '^(\d+)\.(\d+)\.(\d+).(\d+)$','$4');

($Record.RecordData.IPv4Address.ToString()) # Shows the Forward Version of the IP
$Name # Shows the last octet

# Add the new PTR record
Add-DnsServerResourceRecordPtr -Name $name -ZoneName $ReverseZone -ComputerName $ComputerName -PtrDomainName $ptrDomain -WhatIf

}`

@blechinger
Copy link

blechinger commented Feb 11, 2022

I also took a shot at modifying this to fix the IPs and make it a little more user friendly.
This script will prompt the user for which Domain Controller to query and which Forward Lookup Zone to pull from.

I found this useful for reconciling missing Reverse Lookup Zone entries that should've been created from Samba4 DC/DNS forward zones.
The script will not create double entries (tested a few ways) if their information matches exactly.
It will instead throw an error (but continue to the next record).

# Creates ptr records in a reverse lookup zone matching the subnet for each A record in a given forward zone.
# This script assumes a /24 for each RLZ. You should modify the regex for $name and $rzname
# to something that makes sense for you if your IP scheme is set up differently.

# User input asking "which Domain Controller do I use?" and "which Forward Lookup Zone do I query?".
param (
	[Parameter(Mandatory)]
        [string]$domainController,
	[Parameter(Mandatory)]
        [string]$forwardZoneName
)

# Get the DNS A records within the specified FLZ from the specified DC.
$records = Get-DnsServerResourceRecord -ZoneName $forwardZoneName -RRType A -ComputerName $domainController; 
foreach ($record in $records) 
{ 
    # The reverse lookup domain name.  This is the PTR Response.
    # This should look like host.forward.zone.tld or similar.
    $ptrDomain = $record.HostName + '.' + $forwardZoneName; 

    # Grab the last octet of the IP address for the record being processed.
    # When creating the PTR record for a /24 the first three octets are already in place
    # due to the RLZ naming schemes so we only need the last octet.
    $name = ($record.RecordData.IPv4Address.ToString() -replace '^(\d+)\.(\d+)\.(\d+).(\d+)$','$4');

    # Reverse the IP Address for the Zone Name. Leave off the last octet to place in proper /24 subnet.
    # Once again if you're carving your subnets up differently you'll want to change this.
    $rzname = ($record.RecordData.IPv4Address.ToString() -replace '^(\d+)\.(\d+)\.(\d+).(\d+)$','$3.$2.$1') + '.in-addr.arpa';
    
    # Add the new PTR record.
    Add-DnsServerResourceRecordPtr -Name $name -ZoneName $rzname -ComputerName $domainController -PtrDomainName $ptrDomain; 
}

@SweetAsNZ
Copy link

SweetAsNZ commented Feb 17, 2022 via email

@Zoran-Jankov
Copy link

Thanks for creating this very useful tool!

@mcdonamw
Copy link

mcdonamw commented Dec 13, 2022

I'm curious... Was this code put out there because there is no simply way to just "update" a PTR record for an existing A record (as can be done with the DNS MMC snap-in? I know Add-DNSServerResourceRecord has -CreatePTR which will tell the system to create the PTR in the correct RevZone, without having to know the name of the zone. But I don't see any of the *-DNSServerResourceRecords support a similar parameter.

I ask as I have many zones in my environment that have a root zone, as well as some subnets with their own zone that I'd like to collapse down.
E.g. 192.10.in-addr.arpa and 22.192.10.in-addr.arpa

My hope was I could simply delete the subzone, and simply run a command to update the PTR for existing A records. This code requires having to know the reverse zone (and proper hostname with correct number of octets depending on the name of the zone) for creating that PTR. I'd rather have the system do it automatically.

I guess one option is deleting the existing FWD records and re-creating with -CreatePTR, but don't really like the idea of deleting anything either.

@msoler8785
Copy link
Author

@mcdonamw just revisiting it today because I need to use the script. I originally created this because I had to rebuild my PTR zones from pre-existing forward records. I didn't see a way to do this in bulk at the time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment