PowerShell to Replicate UDM-Pro DNS Records to Windows DNS Server
Hello Friends... Thought some of you might find value in this... The following script will SSH to your UDM-Pro, export the DNS data, and parse the data to add host and PTR records in Windows DNS. The script removes any outdated/duplicate records before adding the new record.
Please note you will need the Posh-SSH module installed and SSH enabled on your UDM-Pro.
# Define variables
$UDMProHost = "UDMPro_IP"
$UDMProUsername = "UDMPro_Username"
$UDMProPassword = "UDMPro_Password"
$WindowsDNSServer = "Windows_DNS_IP"
$WindowsDNSZone = "yourdomain.local"
$WindowsDNSReverseZone = "0.168.192.in-addr.arpa"
# Connect to UDM-Pro using SSH
$UDMProCreds = New-Object System.Management.Automation.PSCredential ($UDMProUsername, ($UDMProPassword | ConvertTo-SecureString -AsPlainText -Force))
$UDMProSession = New-SSHSession -ComputerName $UDMProHost -Credential $UDMProCreds
# Export UDM-Pro DNS data
$UDMProDNSExport = Invoke-SSHCommand -SSHSession $UDMProSession -Command "cat /etc/hosts"
# Disconnect SSH session
Remove-SSHSession -SSHSession $UDMProSession
# Parse exported DNS data
$DNSRecords = $UDMProDNSExport.Output -split "`n" | Where-Object {$_ -match "^(\d{1,3}\.){3}\d{1,3}"}
# Create DNS records on Windows DNS server
foreach ($record in $DNSRecords) {
$recordArray = $record -split "\s+"
$ipAddress = $recordArray[0]
$hostname = $recordArray[1]
# Remove existing DNS records with the same hostname
$existingRecordA = Get-DnsServerResourceRecord -ComputerName $WindowsDNSServer -ZoneName $WindowsDNSZone -RRType A -Name $hostname -ErrorAction SilentlyContinue
if ($existingRecordA) {
Remove-DnsServerResourceRecord -ComputerName $WindowsDNSServer -ZoneName $WindowsDNSZone -InputObject $existingRecordA -Force
}
# Add DNS A record to Windows DNS server
Add-DnsServerResourceRecordA -ComputerName $WindowsDNSServer -ZoneName $WindowsDNSZone -Name $hostname -IPv4Address $ipAddress -AllowUpdateAny
# Remove existing PTR records with the same IP address
$ptrName = (($ipAddress.Split(".") | Select-Object -Last 1) -join ".") + "." + $WindowsDNSReverseZone
$existingRecordPTR = Get-DnsServerResourceRecord -ComputerName $WindowsDNSServer -ZoneName $WindowsDNSReverseZone -RRType PTR -Name $ptrName -ErrorAction SilentlyContinue
if ($existingRecordPTR) {
Remove-DnsServerResourceRecord -ComputerName $WindowsDNSServer -ZoneName $WindowsDNSReverseZone -InputObject $existingRecordPTR -Force
}
# Add DNS PTR record to Windows DNS server
Add-DnsServerResourceRecordPtr -ComputerName $WindowsDNSServer -ZoneName $WindowsDNSReverseZone -Name $ptrName -PtrDomainName "$hostname.$WindowsDNSZone" -AllowUpdateAny
}
Comments
Post a Comment