Skip to content

Instantly share code, notes, and snippets.

@MarkZhangTW
Last active February 1, 2021 13:45
Show Gist options
  • Save MarkZhangTW/87ab72ba5460b8609e44b5d7e603edee to your computer and use it in GitHub Desktop.
Save MarkZhangTW/87ab72ba5460b8609e44b5d7e603edee to your computer and use it in GitHub Desktop.
Update CloudFlare DNS records with PowerShell
$CF = @{}
$CF.Endpoint = "https://api.cloudflare.com/client/v4/"
# Replace <API Token> to your CloudFlare API Token
# You can create your API Token from https://dash.cloudflare.com/profile/api-tokens
$CF.APIToken = "Bearer <API Token>"
# Replace <zone id> and <record id>
$CF.UpdateDNSRecord = @{Method = "PUT"; object = "zones/<zone id>/dns_records/<record id>"}
$PublicIP = (Get-NetIPAddress -InterfaceAlias "<Interface Alias>" -AddressFamily IPv4).IPAddress
$target = $CF.UpdateDNSRecord
$Uri = $CF.Endpoint + $target.object
$Headers = @{Authorization = $CF.APIToken}
$content = @{}
$content.type = "A"
$content.name = "<domain name>"
$content.content = $PublicIP
$content.ttl = 120
$content = ConvertTo-Json $content
# You can remomve UseBasicParsing flag if your PowerShell version >= 6.0.0
$response = Invoke-WebRequest -UseBasicParsing -Method $target.Method -Uri $Uri -Headers $Headers -Body $content
$response.Content | ConvertFrom-Json | ConvertTo-Json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment