Skip to content

Instantly share code, notes, and snippets.

@davidlu1001
Last active June 20, 2024 11:46
Show Gist options
  • Save davidlu1001/6f0e80fdfade310e014227e922d40635 to your computer and use it in GitHub Desktop.
Save davidlu1001/6f0e80fdfade310e014227e922d40635 to your computer and use it in GitHub Desktop.
Set CName TTL
param(
[Parameter(Mandatory=$false)]
[string]$dnsServer,
[Parameter(Mandatory=$false)]
[string]$lookupZone,
[Parameter(Mandatory=$false)]
[string[]]$cnameRecords,
[Parameter(Mandatory=$false)]
[string[]]$CnameListFilePaths
)
# Load cname records from files if any are provided
foreach ($path in $CnameListFilePaths) {
if (Test-Path $path) {
$cnameRecords += Get-Content $path
} else {
Write-Warning "File not found: $path"
}
}
$TTL = [System.TimeSpan]::FromMinutes(15)
$zeroTTL = [System.TimeSpan]::Zero
foreach ($cname in $cnameRecords) {
try {
$dnsRecord = Get-DnsServerResourceRecord -Name $cname -ZoneName $lookupZone -ComputerName $dnsServer -RRType "CName"
if ($dnsRecord -and ($dnsRecord.TimeToLive -eq $zeroTTL -or $dnsRecord.TimeToLive -eq $null)) {
Write-Output "Updating TTL for $cname..."
$modifiedRecord = $dnsRecord.Clone()
$modifiedRecord.TimeToLive = $TTL
Set-DnsServerResourceRecord -OldInputObject $dnsRecord -NewInputObject $modifiedRecord -ZoneName $lookupZone -ComputerName $dnsServer
Write-Output "TTL updated to 15 minutes for $cname."
} else {
Write-Output "TTL for $cname is already set or not zero."
}
} catch {
Write-Error "Error processing $cname : $_"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment