Last active
June 20, 2024 11:46
-
-
Save davidlu1001/6f0e80fdfade310e014227e922d40635 to your computer and use it in GitHub Desktop.
Set CName TTL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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