-
-
Save msoler8785/498332c622f93ace02b5d05e47845001 to your computer and use it in GitHub Desktop.
# 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; | |
} |
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;
}
Thanks for creating this very useful tool!
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.
@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.
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;
}`