Home » IT - Microsoft » Create DNS PTR record if A record exists

Create DNS PTR record if A record exists

Escribe tu dirección de correo electrónico para suscribirte a este blog, y recibir notificaciones de nuevos mensajes por correo.

Join 5 other followers

March 2016
M T W T F S S
« Feb   May »
 123456
78910111213
14151617181920
21222324252627
28293031  

NO! A LOS TOROS

Disclaimer

All messages posted to this blog are provided "AS IS" with no warranties, and confer no rights. The content of this site are personal opinions and might not represent the Microsoft Corporation view. Regarding any sample code that we provide: This Sample Code is provided for the purpose of illustration only and is not intended to be used in a production environment. THIS SAMPLE CODE AND ANY RELATED INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. This blog serves 2 purposes. Firstly, I want to share information with other IT pros about the technologies we work with and how to solve problems we often face. Secondly, I use my blog as a notebook. There's so much to learn and remember in our jobs that it's impossible to keep up. By blogging, I have a notebook that I can access from anywhere. Anything you do to your IT infrastructure, applications, services, computer or anything else is 100% down to your own responsibility and liability. Mcselles bears no responsibility or liability for anything you do. Please independently confirm anything you read on this blog before doing whatever you decide to do.
Advertisements

Create DNS PTR record if A record exists.

This script will help you to have a DNS PTR record for each existing A record in your DNS zone.

If a A record has been created in the DNS zone ($dnsDomainName variable) without a PTR record, the script detect it and the PTR record is created in the correct DNS reverse zone.

Script :

$dnsServer = "dnsServer01.local.net"
$dnsDomainName = "local.net"

function CreatePTR($dnsServer,$reverse_zone,$reverse_ip,$hostname) {
    Invoke-WmiMethod -Name CreateInstanceFromPropertyData -Class MicrosoftDNS_PTRType `
    -Namespace root\MicrosoftDNS -ArgumentList "$reverse_zone","$dnsServer","$reverse_ip","$hostname" `
    -ComputerName $dnsServer
}

$record_R_list = gwmi -namespace "root\MicrosoftDNS" -class MicrosoftDNS_PTRtype -ComputerName $dnsServer | % {$_.recorddata}
$record_A_list = gwmi -namespace "root\MicrosoftDNS" -class MicrosoftDNS_Atype -ComputerName $dnsServer |select ownername,IPaddress | ? { $_.ownername -like "*.$dnsDomainName" }
$reverse_zone_list = gwmi MicrosoftDNS_Zone -Namespace ‘root\MicrosoftDNS’ -filter "reverse=true" -computer $dnsServer | % {$_.name}

foreach ($a_record in $record_A_list) {
    $hostname = $a_record.ownername+"."
    $ipaddress = $a_record.IPaddress
    if ($record_R_list -notcontains $hostname) {
        Write-Host -NoNewline "The following host does not have a valid reverse record in DNS : " $hostname

        $PingStatus = Gwmi Win32_PingStatus -Filter "Address = ‘$hostname’" | Select-Object StatusCode
        If ($PingStatus.StatusCode -eq 0){
            Write-Host " (online)" -Fore "Green"
            $arr = $ipaddress.split(".")
            [array]::Reverse($arr)
            $reverse_ip = ($arr -join ‘.’) + ".in-addr.arpa"

            #detect the correct dns reverse lookup zone
            $arr_rvr = $reverse_ip.Split(".")
            $arr_rvr1 = $arr_rvr[1] + "." + $arr_rvr[2] + "." + $arr_rvr[3] + ".in-addr.arpa"
            $arr_rvr2 = $arr_rvr[2] + "." + $arr_rvr[3] + ".in-addr.arpa"
            $arr_rvr3 = $arr_rvr[3] + ".in-addr.arpa"
            if ($reverse_zone_list -contains $arr_rvr1){
                Write-Host $arr_rvr1 " exists in DNS reverse lookup zones"
                Write-Host $reverse_ip
                CreatePTR $dnsServer $arr_rvr1 $reverse_ip $hostname
            }
            elseif ($reverse_zone_list -contains $arr_rvr2){
                Write-Host $arr_rvr2 " exists in DNS reverse lookup zones"
                Write-Host $reverse_ip
                CreatePTR $dnsServer $arr_rvr2 $reverse_ip $hostname
            }
            elseif ($reverse_zone_list -contains $arr_rvr3) {
                Write-Host $arr_rvr3 " exists in DNS reverse lookup zones"
                Write-Host $reverse_ip
                CreatePTR $dnsServer $arr_rvr3 $reverse_ip $hostname
            }
            else {
                Write-Host "Reverse lookup zone does not exist. Cannot create the PTR record"
            }
        }
        Else {
            Write-Host " (offline)"  -Fore "Red"
        }
    }
}

Source: https://www.shellandco.net/create-dns-ptr-record-if-a-record-exists/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Microsoft on the Issues

News and perspectives on legal, public policy and citizenship topics

Mike Crowley's Whiteboard

“There are no limits to what you can accomplish when you are supposed to be doing something else."

T.B.D.

There Be Dragons

Ken Cenerelli

My life in software development

VMware, Windows, Virtualization (Servers & Desktops)

VMware, Windows, Virtualization (Servers & Desktops)

Just a random "Microsoft Server / Client Tech" info..

"Feeding Your Training and Technology Obsessions"

WordPress.com

WordPress.com is the best place for your personal blog or business site.

DocSharing

Documentación técnica, notas y apuntes sobre Administración de Sistemas, Servidores, Redes y más

Microsoft Taste

Mary's Blog

%d bloggers like this: