Created
July 21, 2024 09:39
-
-
Save dw5/6e93729d95435b89e8363d6633c4caae to your computer and use it in GitHub Desktop.
cloudflare dns bulk add update remove
This file contains hidden or 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
# Cloudflare API Token | |
$apiToken = "" | |
#Region Token Test | |
## This block verifies that your API key is valid. | |
## If not, the script will terminate. | |
$headers = @{ | |
"Authorization" = "Bearer $($apiToken)" | |
"Content-Type" = "application/json" | |
} | |
$auth_result = Invoke-RestMethod -Method GET -Uri "https://api.cloudflare.com/client/v4/user/tokens/verify" -Headers $headers | |
if (-not($auth_result.result)) { | |
Write-Output "API token validation failed. Error: $($auth_result.errors.message). Terminating script." | |
# Exit script | |
return | |
} | |
Write-Output "API token validation [$($apiToken)] success. $($auth_result.messages.message)." | |
#EndRegion | |
# Import CSV | |
$csv = Import-Csv -Path "db.csv" | |
foreach ($record in $csv) { | |
$zoneId = $record.zone_id | |
$recordType = $record.record_type | |
$name = $record.name | |
$content = $record.content | |
$ttl = $record.ttl | |
$proxied = $record.proxied -eq "true" | |
$priority = if ($record.record_type -eq "MX") { $record.priority } else { $null } | |
$headers = @{ | |
"Authorization" = "Bearer $apiToken" | |
"Content-Type" = "application/json" | |
} | |
$body = @{ | |
type = $recordType | |
name = $name | |
content = $content | |
ttl = [int]$ttl | |
proxied = $proxied | |
} | |
if ($recordType -eq "MX" -and $priority) { | |
$body.priority = [int]$priority | |
} | |
$bodyJson = $body | ConvertTo-Json | |
switch ($record.action.ToLower()) { | |
"add" { | |
$response = Invoke-RestMethod -Uri "https://api.cloudflare.com/client/v4/zones/$zoneId/dns_records" -Method Post -Headers $headers -Body $bodyJson | |
Write-Output "Add Response: $($response | ConvertTo-Json -Depth 10)" | |
} | |
"update" { | |
$dnsRecordId = (Invoke-RestMethod -Uri "https://api.cloudflare.com/client/v4/zones/$zoneId/dns_records?type=$recordType&name=$name" -Headers $headers).result[0].id | |
$response = Invoke-RestMethod -Uri "https://api.cloudflare.com/client/v4/zones/$zoneId/dns_records/$dnsRecordId" -Method Put -Headers $headers -Body $bodyJson | |
Write-Output "Update Response: $($response | ConvertTo-Json -Depth 10)" | |
} | |
"delete" { | |
$dnsRecordId = (Invoke-RestMethod -Uri "https://api.cloudflare.com/client/v4/zones/$zoneId/dns_records?type=$recordType&name=$name" -Headers $headers).result[0].id | |
$response = Invoke-RestMethod -Uri "https://api.cloudflare.com/client/v4/zones/$zoneId/dns_records/$dnsRecordId" -Method Delete -Headers $headers | |
Write-Output "Delete Response: $($response | ConvertTo-Json -Depth 10)" | |
} | |
default { | |
Write-Output "Invalid action: $($record.action)" | |
} | |
} | |
} | |
# TODO: Alert if there were some failures, and show which ones |
This file contains hidden or 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
action | zone_id | record_type | name | content | ttl | proxied | priority |
---|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment