#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." } } }