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