Add/delete A/PTR records in windows DNS

I am trying to use following scripts to add/delete A and PTR records in DNS

but not sure if it will work

param(

[Parameter(Mandatory=$true)]

[string]$ARecord,

[Parameter(Mandatory=$true)]

[string]$zone,

[Parameter(Mandatory=$true)]

[string]$DnsServer

)

$ARecordcheck = Get-DnsServerResourceRecord -ZoneName $zone -RRType A -Name $ARecord -ComputerName $DnsServer -ErrorAction SilentlyContinue

Check if A Record Exist

if ($ARecordcheck -eq $null) {

Write-Error “A record $ARecord does not exist in DNS zone.”

}

$FQDN = $ARecord + “.” + $zone

Get IP Address from DNS

$IP = Resolve-DnsName -Name $ARecord -Type A | Select-Object -ExpandProperty IPAddress

$PTRLastOctet = $IP.Split(‘.’)[-1]

Get reverse lookup zone and IP

$reverselookupIP = (Resolve-DnsName -type PTR $IP | select -ExpandProperty Name)

$reversezone = $reverselookupIP.Substring(($reverselookupIP.IndexOf(‘.’) + 1))

$PtrRecord = Get-DnsServerResourceRecord -ZoneName $reversezone -RRType PTR -ComputerName $DnsServer -Name $PTRLastOctet

if ($PtrRecord -eq $null) {

Write-Error “PTR record $PtrRecord does not exist in DNS zone $reversezone”

exit 1

}

ping test

$result = Test-NetConnection $IP -InformationLevel Quiet

if (-not $result) {

write-host “Proceeding to delete DNS Record…”

Remove-DnsServerResourceRecord -ZoneName $zone -name $ARecord -ComputerName $DNSServer -RRType A -Confirm:$false

Remove-DnsServerResourceRecord -ZoneName $reversezone -ComputerName $DnsServer -RRType PTR -RecordData $FQDN -Name $PTRLastOctet -Confirm:$false

}

else {

write-host "IP is alive... PLeaase check"

exit

} 

param(

[Parameter(Mandatory=$true)]

[string]$ARecord,

[Parameter(Mandatory=$true)]

[string]$IP,

[Parameter(Mandatory=$true)]

[string]$zone,

[Parameter(Mandatory=$true)]

[string]$DnsServer

)

ping test

$result = Test-NetConnection $IP -InformationLevel Quiet

if ($result) {

Write-Error "IP is alive. Please check or use different IP"

exit

}

#Check If A Record Exist

Check if A record exist

$ExistingARecord = Get-DnsServerResourceRecord -ZoneName $Zone -Name $ARecord -RRType A -ErrorAction SilentlyContinue -ComputerName $DnsServer

if ($ExistingARecord) {

Write-Warning "A record '$ARecord.$Zone' already exists."

Exit

}

else {

# Create A record

Try {

    Add-DnsServerResourceRecordA -Name $ARecord -ZoneName $Zone -IPv4Address $IP -ErrorAction Stop -ComputerName $DnsServer

}

Catch {

    Write-Error "Failed to create A record '$ARecord.$Zone'. Error: $($_.Exception.Message)"

    Exit

}

}

FQDN

$FQDN = $ARecord + “.” + $zone

Find the reverse DNS zone from IP

$First3_Octets = $IP.Split(“.”)[0…2] -join “.”

$octets = $first3octets.split(‘.’)

$reverseoctets = $octets -join ‘.’

$reversezone = $reverseoctets + “.in-addr.arpa”

$PTRLastOctet = $IP.Split(‘.’)[-1]

$ExistingPTRRecord = Get-DnsServerResourceRecord -ZoneName $reversezone -RRType PTR -ComputerName $DnsServer -Name $PTRLastOctet

if ($ExistingPTRRecord) {

Write-Warning "PTR record '$RecordName.$ZoneName' already exists."

Exit

}

else {

# Create PTR record

Try {

    Add-DnsServerResourceRecordA -Name $ARecord -ZoneName $Zone -IPv4Address $IP -ErrorAction Stop -ComputerName $DnsServer

    Add-DnsServerResourceRecordPtr -ZoneName $reversezone -Name $PTRLastOctet -PtrDomainName $FQDN -ComputerName $DnsServer

}

Catch {

    Write-Error "Failed to create PTR record $FQDN Error: $($_.Exception.Message)"

    Exit

Tdubb123,
Welcome to the forum. :wave:t3:

Wow … that’s a lot of code. And it’s all unformatted. :smirk:

Please go back, edit your code once again and fix the formatting of your code …

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink:

Regardless of that … you forgot to ask a specific question. :point_up_2:t3:

Please keep in mind … we do not offer free code reviews or genereal consulting.

param(

[Parameter(Mandatory = $true)]
[string]$ARecord,
[Parameter(Mandatory = $true)]
[string]$IP,
[Parameter(Mandatory = $true)]
[string]$zone,
[Parameter(Mandatory = $true)]
[string]$DnsServer

)

ping test

$result = Test-NetConnection $IP -InformationLevel Quiet
if ($result) {
Write-Error “IP is alive. Please check or use different IP”
exit
}

#Check If A Record Exist

$ExistingARecord = Get-DnsServerResourceRecord -ZoneName $Zone -Name $ARecord -RRType A -ErrorAction SilentlyContinue -ComputerName $DnsServer

if ($ExistingARecord) {
Write-Warning “A record ‘$ARecord.$Zone’ already exists.”
Exit
}

else {

# Create A record
Try {
    Add-DnsServerResourceRecordA -Name $ARecord -ZoneName $Zone -IPv4Address $IP -ErrorAction Stop -ComputerName $DnsServer
}

Catch {
    Write-Error "Failed to create A record '$ARecord.$Zone'. Error: $($_.Exception.Message)"
    Exit
}

}

FQDN

$FQDN = $ARecord + “.” + $zone

Find the reverse DNS zone from IP

$First3_Octets = $IP.Split(“.”)[0…2] -join “.”
$octets = $first3_octets.split(‘.’)
[Array]::Reverse($octets)
$reverseoctets = $octets -join ‘.’
$reversezone = $reverseoctets + “.in-addr.arpa”
$PTRLastOctet = $IP.Split(‘.’)[-1]

$ExistingPTRRecord = Get-DnsServerResourceRecord -ZoneName $reversezone -RRType PTR -ComputerName $DnsServer -Name $PTRLastOctet

if ($ExistingPTRRecord) {
Write-Warning “PTR record ‘$RecordName.$ZoneName’ already exists.”
Exit
}

else {

# Create PTR record

Try {

    Add-DnsServerResourceRecordA -Name $ARecord -ZoneName $Zone -IPv4Address $IP -ErrorAction Stop -ComputerName $DnsServer
    Add-DnsServerResourceRecordPtr -ZoneName $reversezone -Name $PTRLastOctet -PtrDomainName $FQDN -ComputerName $DnsServer
}
Catch {
    Write-Error "Failed to create PTR record $FQDN Error: $($_.Exception.Message)"
    Exit
}

}

param(

    [Parameter(Mandatory = $true)]
    [string]$ARecord,
    [Parameter(Mandatory = $true)]
    [string]$IP,
    [Parameter(Mandatory = $true)]
    [string]$zone,
    [Parameter(Mandatory = $true)]
    [string]$DnsServer
)

 
# ping test

$result = Test-NetConnection $IP -InformationLevel Quiet
if ($result) {
    Write-Error "IP is alive. Please check or use different IP"
    exit
}

#Check If A Record Exist

$ExistingARecord = Get-DnsServerResourceRecord -ZoneName $Zone -Name $ARecord -RRType A -ErrorAction SilentlyContinue -ComputerName $DnsServer

if ($ExistingARecord) {
    Write-Warning "A record '$ARecord.$Zone' already exists."
    Exit
}

else {

    # Create A record
    Try {
        Add-DnsServerResourceRecordA -Name $ARecord -ZoneName $Zone -IPv4Address $IP -ErrorAction Stop -ComputerName $DnsServer
    }

    Catch {
        Write-Error "Failed to create A record '$ARecord.$Zone'. Error: $($_.Exception.Message)"
        Exit
    }
}

 

 

# FQDN 

$FQDN = $ARecord + "." + $zone

# Find the reverse DNS zone from IP
 
$First3_Octets = $IP.Split(".")[0..2] -join "."
$octets = $first3_octets.split('.')
[Array]::Reverse($octets)
$reverseoctets = $octets -join '.'
$reversezone = $reverseoctets + ".in-addr.arpa"
$PTRLastOctet = $IP.Split('.')[-1]

 

 

$ExistingPTRRecord = Get-DnsServerResourceRecord -ZoneName $reversezone -RRType PTR -ComputerName $DnsServer -Name $PTRLastOctet

 

if ($ExistingPTRRecord) {
    Write-Warning "PTR record '$RecordName.$ZoneName' already exists."
    Exit
}

else {

    # Create PTR record

    Try {

        Add-DnsServerResourceRecordA -Name $ARecord -ZoneName $Zone -IPv4Address $IP -ErrorAction Stop -ComputerName $DnsServer
        Add-DnsServerResourceRecordPtr -ZoneName $reversezone -Name $PTRLastOctet -PtrDomainName $FQDN -ComputerName $DnsServer
    }
    Catch {
        Write-Error "Failed to create PTR record $FQDN Error: $($_.Exception.Message)"
        Exit
    }
}

param(

    [Parameter(Mandatory = $true)]
    [string]$ARecord,
    [Parameter(Mandatory = $true)]
    [string]$IP,
    [Parameter(Mandatory = $true)]
    [string]$zone,
    [Parameter(Mandatory = $true)]
    [string]$DnsServer
)


# ping test

$result = Test-NetConnection $IP -InformationLevel Quiet
if ($result) {
    Write-Error "IP is alive. Please check or use different IP"
    exit
}

# Check if A record exist

$ExistingARecord = Get-DnsServerResourceRecord -ZoneName $Zone -Name $ARecord -RRType A -ErrorAction SilentlyContinue -ComputerName $DnsServer
if ($ExistingARecord) {
    Write-Warning "A record '$ARecord.$Zone' already exists."
    Exit
}

else {
    # Create A record
    Try {
        Add-DnsServerResourceRecordA -Name $ARecord -ZoneName $Zone -IPv4Address $IP -ErrorAction Stop -ComputerName $DnsServer
    }
    Catch {
        Write-Error "Failed to create A record '$ARecord.$Zone'. Error: $($_.Exception.Message)"
        Exit
    }
}

 

 

# FQDN 

$FQDN = $ARecord + "." + $zone
 
# Find the reverse DNS zone from IP

$First3_Octets = $IP.Split(".")[0..2] -join "."
$octets = $first3octets.split('.')
[Array]::Reverse($octets)
$reverseoctets = $octets -join '.'
$reversezone = $reverseoctets + ".in-addr.arpa"
$PTRLastOctet = $IP.Split('.')[-1]

 

 

$ExistingPTRRecord = Get-DnsServerResourceRecord -ZoneName $reversezone -RRType PTR -ComputerName $DnsServer -Name $PTRLastOctet

if ($ExistingPTRRecord) {
    Write-Warning "PTR record '$RecordName.$ZoneName' already exists."
    Exit
}

else {

    # Create PTR record
    Try {
        Add-DnsServerResourceRecordA -Name $ARecord -ZoneName $Zone -IPv4Address $IP -ErrorAction Stop -ComputerName $DnsServer
        Add-DnsServerResourceRecordPtr -ZoneName $reversezone -Name $PTRLastOctet -PtrDomainName $FQDN -ComputerName $DnsServer
    }

    Catch {
        Write-Error "Failed to create PTR record $FQDN Error: $($_.Exception.Message)"
        Exit
    }
}```
param(

    [Parameter(Mandatory = $true)]
    [string]$ARecord,
    [Parameter(Mandatory = $true)]
    [string]$zone,
    [Parameter(Mandatory = $true)]
    [string]$DnsServer
)


$ARecordcheck = Get-DnsServerResourceRecord -ZoneName $zone -RRType A -Name $ARecord -ComputerName $DnsServer -ErrorAction SilentlyContinue

# Check if A Record Exist

if ($ARecordcheck -eq $null) {
    Write-Error "A record $ARecord does not exist in DNS zone."
}


$FQDN = $ARecord + "." + $zone

# Get IP Address from DNS
$IP = Resolve-DnsName -Name $ARecord -Type A | Select-Object -ExpandProperty  IPAddress
$PTRLastOctet = $IP.Split('.')[-1]
 

# Get reverse lookup zone and IP

$reverselookupIP = (Resolve-DnsName -type PTR $IP | select -ExpandProperty Name)
$reversezone = $reverselookupIP.Substring(($reverselookupIP.IndexOf('.') + 1))

$PtrRecord = Get-DnsServerResourceRecord -ZoneName $reversezone -RRType PTR -ComputerName $DnsServer -Name $PTRLastOctet

if ($PtrRecord -eq $null) {

    Write-Error "PTR record $PtrRecord does not exist in DNS zone $reversezone"
    exit 1
}

 

 

# ping test

$result = Test-NetConnection $IP -InformationLevel Quiet

if (-not $result) {
    write-host "Proceeding to delete DNS Record.."
    Remove-DnsServerResourceRecord -ZoneName $zone -name $ARecord -ComputerName $DNSServer -RRType A -Confirm:$false
    Remove-DnsServerResourceRecord -ZoneName $reversezone -ComputerName $DnsServer -RRType PTR -RecordData $FQDN -Name $PTRLastOctet -Confirm:$false
}

else {
    write-host "IP is alive... PLeaase check"
    exit
}

I actually asked you to edit your initial post … :smirk:

When one of your posts get’s on hold for moderation please give us a little time to evaluate. It does not make any sense to post it again and again.

OK. Now that you managed to post your code properly formatted, what is your question?

And again …we do not offer free code reviews or genereal consulting.

ok here is the one I am having issues with. DNS A record is getting crated but not PTR record.

param(

  [Parameter(Mandatory=$true)]
  [string]$ARecord,
  [Parameter(Mandatory=$true)]
  [string]$IP,
  [Parameter(Mandatory=$true)]
  [string]$zone,
  [Parameter(Mandatory=$true)]
  [string]$DnsServer
)

# ping test
$result = Test-NetConnection $IP -InformationLevel Quiet

if ($result) {
    Write-Error "IP is alive. Please check or use different IP"
    exit
    }

#Check If A Record Exist
 
$ExistingARecord = Get-DnsServerResourceRecord -ZoneName $Zone -Name $ARecord -RRType A -ErrorAction SilentlyContinue -ComputerName $DnsServer
if ($ExistingARecord) {
    Write-Warning "A record '$ARecord.$Zone' already exists."
    Exit
}

else {
    # Create A record
    Try {
        Add-DnsServerResourceRecordA -Name $ARecord -ZoneName $Zone -IPv4Address $IP -ErrorAction Stop -ComputerName $DnsServer
    }

    Catch {
        Write-Error "Failed to create A record '$ARecord.$Zone'. Error: $($_.Exception.Message)"
        Exit
    }
}


# FQDN 

$FQDN = $ARecord + "." + $zone
 
# Find the reverse DNS zone from IP

$First3_Octets =  $IP.Split(".")[0..2] -join "."
$octets = $first3octets.split('.')
[Array]::Reverse($octets)
$reverseoctets = $octets -join '.'
$reversezone = $reverseoctets + ".in-addr.arpa"
$PTRLastOctet = $IP.Split('.')[-1]

 
$ExistingPTRRecord = Get-DnsServerResourceRecord -ZoneName $reversezone -RRType PTR -ComputerName $DnsServer -Name $PTRLastOctet

if ($ExistingPTRRecord) {
    Write-Warning "PTR record '$RecordName.$ZoneName' already exists."
    Exit
}
else {
    # Create PTR record
    Try {
        Add-DnsServerResourceRecordA -Name $ARecord -ZoneName $Zone -IPv4Address $IP -ErrorAction Stop -ComputerName $DnsServer
        Add-DnsServerResourceRecordPtr -ZoneName $reversezone -Name $PTRLastOctet -PtrDomainName $FQDN -ComputerName $DnsServer
    }

    Catch {
        Write-Error "Failed to create PTR record $FQDN Error: $($_.Exception.Message)"
        Exit
    }
}
 

Since this is a pretty specific topic/issue and the least of us will have a test environment to check on that you will have to help us.

  1. For a try catch block to work properly the error happening inside of the try block has to be a termintaing error in order for the catch block to … catch it. So if the cmdlet you’re using inside of the try block does not raise a terminating error by default you have to force it to with the parameter -ErrorAction Stop or with setting the preference variable $ErrorActionPreference to Stop beforehand for the whole script or at least for the try catch block.

  2. Could you set the PTR record manually outside of your script by providing the desired values for the given parameters? Do you get any errors then?