From ce6c3a33a40cb54b97fc8680ccc1573b39bf9a75 Mon Sep 17 00:00:00 2001 From: ssn Date: Thu, 12 Mar 2026 20:46:16 +0100 Subject: [PATCH] Added PowerShell version --- fixDNS.ps1 | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 fixDNS.ps1 diff --git a/fixDNS.ps1 b/fixDNS.ps1 new file mode 100644 index 0000000..89bba44 --- /dev/null +++ b/fixDNS.ps1 @@ -0,0 +1,39 @@ +#Dealing with my Cloudflare domains and my dynamic IP +$progressPreference = "silentlyContinue" +$timeStamp = (get-Date).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss") +$cfEmail = "cloudflare@sorensiim.dk" +$cloudflareAccountId = "5781ba314730623cc65f8e943e1bf215" +$cfToken = "Md8Na4WkayyONozTp8i7OvS2PdFt63fdP1ZR_lTt" +$myIp = curl "api.ipify.org" #Curl is easier for this one +$cfAPIBaseURI = "https://api.cloudflare.com/client/v4/" +#Get an auth token +$cfHeaders = @{ + "Authorization" = "Bearer $cfToken" +} + +#Pull the DNS zones +$zones = ((Invoke-WebRequest -URI "$($cfAPIBaseURI)zones" -Method GET -Headers $cfHeaders -UseBasicParsing).content | ConvertFrom-JSON).Result +#$zones + +#Get the DNS records for the zones, except the TailScale records +$zones | Foreach-Object{ + $zoneId = $_.id + $dnsRecords = ((Invoke-WebRequest -URI "$($cfAPIBaseURI)zones/$($_.id)/dns_records" -Method GET -Headers $cfHeaders -UseBasicParsing).content | ConvertFrom-JSON).Result | Where-Object{($_.type -eq "A") -and ($_.content -notMatch "^100")} | Select-Object id, name, type, content + #$dnsRecords | Where-Object{($_.type -eq "A") -and ($_.content -notMatch "^100")} | Select-Object id, name, type, content + #break + #Update the DNS record to use the correct IP + $dnsRecords | where-Object{$_.content -ne $myIp} | Foreach-object{ + $oldIp = $_.content + $recordBody = @{ + comment = "$TimeStamp | Updated automatically from $oldIp to $myIp" + content = "$myIp" + } | ConvertTo-JSON + $ipFix = Invoke-WebRequest -URI "$($cfAPIBaseURI)zones/$($zoneId)/dns_records/$($_.id)" -Method PATCH -Body $recordBody -Headers $cfHeaders + If($ipFix.statuscode -match "20."){ + Write-output "Successfully changed the A-record $($_.name) from $oldIp to $myIp." + } + If($ipFix.statuscode -notMatch "20."){ + Write-error "Failed to change the A-record $($_.name) from $oldIp to $myIp." + } + } +} \ No newline at end of file