Skip to content

Instantly share code, notes, and snippets.

@msoler8785
Last active July 4, 2024 00:57
Show Gist options
  • 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;
}
@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