Skip to content

Instantly share code, notes, and snippets.

@amg1127
Last active April 18, 2022 06:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amg1127/c82cf89d51433c8ba00deafee6bff37c to your computer and use it in GitHub Desktop.
Save amg1127/c82cf89d51433c8ba00deafee6bff37c to your computer and use it in GitHub Desktop.
Modify DNS records with idempotence
function Adjust-DNSServerResourceRecord {
param(
[Parameter(Mandatory=$true)][string]$ZoneName,
[Parameter(Mandatory=$true)][string]$Name,
[Parameter(Mandatory=$true)][string]$RRType,
[Parameter(Mandatory=$true)]$Data,
$TimeToLive = $null
)
# Initialize local variables
$dnsRecordTypesRetrieval = @{
'CNAME' = 'HostNameAlias';
'A' = @('IPv4Address', 'IPAddressToString')
}
$dnsRecordTypesCreation = @{
'CNAME' = @{
"CName" = $true;
"HostNameAlias" = $null
};
'A' = @{
"A" = $true;
"IPv4Address" = $null
}
}
$dnsRecordTypesCreationKeys = @{
'CNAME' = 'HostNameAlias';
'A' = 'IPv4Address'
}
# Find the DNS server address and the default RR TTL
$dnsSOARecords = @(Resolve-DnsName -Type SOA -Name $ZoneName)
$dnsServer = $dnsSOARecords[0].PrimaryServer
$dnsTTL = $dnsSOARecords[0].DefaultTTL
# Validate function arguments
if (-not ($dnsRecordTypesRetrieval.ContainsKey($RRType))) {
throw ("Invalid RRtype: '" + $RRType + "'")
}
if (-not $TimeToLive) {
$TimeToLive = New-TimeSpan -Seconds $dnsTTL
}
# Check whether or not DNS records need update
try {
$dnsRecords = Get-DnsServerResourceRecord `
-ComputerName $dnsServer `
-ZoneName $ZoneName `
-Name $Name `
-RRType $RRType `
-Node
if ((@($dnsRecords) | Where-Object { $_.TimeToLive -ne $TimeToLive }).length -eq 0) {
$dnsRecords = $dnsRecords | Select -ExpandProperty RecordData
foreach ($property in @($dnsRecordTypesRetrieval[$RRType])) {
$dnsRecords = $dnsRecords | Select-Object -ExpandProperty $property
}
if ($dnsRecords) {
if (-not (Compare-Object -ReferenceObject $dnsRecords -DifferenceObject $Data)) {
return
}
}
}
} finally {
}
# DNS records do need update. Delete old records...
try {
Get-DnsServerResourceRecord `
-ComputerName $dnsServer `
-ZoneName $ZoneName `
-Name $Name `
-Node | `
Select -Unique -ExpandProperty RecordType | ForEach-Object {
try {
Remove-DnsServerResourceRecord `
-ComputerName $dnsServer `
-ZoneName $ZoneName `
-Name $Name `
-RRType $_ `
-Force
Write-Warning ("Removed '" + $Name + "." + $ZoneName + " " + $_ + "'...")
} catch {
Write-Error $_
}
}
} finally {
}
# ...and create new ones.
foreach ($item in @($Data)) {
$params = $dnsRecordTypesCreation[$RRType]
$params[$dnsRecordTypesCreationKeys[$RRType]] = $item
try {
$newDNSRecord = Add-DnsServerResourceRecord `
-ComputerName $dnsServer `
-ZoneName $ZoneName `
-Name $Name `
@params `
-TimeToLive $TimeToLive `
-PassThru
Write-Warning ("Added '" + $Name + "." + $ZoneName + " " + $newDNSRecord.TimeToLive.TotalSeconds + " " + $RRType + " " + $item + "'...")
} catch {
Write-Error $_
}
}
}
Adjust-DNSServerResourceRecord -ZoneName adatum.com -Name test -RRType A -Data 172.16.1.245,172.24.0.1
Adjust-DNSServerResourceRecord -ZoneName adatum.com -Name testalias -RRType CNAME -Data test.adatum.com. -TimeToLive 0:2:0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment