Skip to content

Instantly share code, notes, and snippets.

@noelbundick
Last active January 13, 2019 03:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noelbundick/e3d3636e54eb40fe4d543a5e89e99a3c to your computer and use it in GitHub Desktop.
Save noelbundick/e3d3636e54eb40fe4d543a5e89e99a3c to your computer and use it in GitHub Desktop.
Register w/ Cloudflare DNS

Dynamic DNS w/ Cloudflare + PowerShell + Windows Scheduled Tasks

Copy registerCloudflare.ps1 to C:\tools\registerCloudflare.ps1

Scheduled Task

Pick a Trigger that makes sense for you. Some examples:

  • At computer startup
  • Every 6 hours
  • When connecting to the network

Use the following for Actions

Program/script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
-Command "& 'C:\tools\registerCloudflare.ps1' -Email 'user@example.com' -ApiKey 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' -Domain 'example.com' -Name 'myfriendlyname'"
MIT License
Copyright (c) 2018 Noel Bundick
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Param(
[Parameter(Mandatory = $true)]
[string] $Email,
[Parameter(Mandatory = $true)]
[string] $ApiKey,
[Parameter(Mandatory = $true)]
[string] $Domain,
[Parameter(Mandatory = $true)]
[string] $Name
)
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$headers = @{
"Content-Type"="application/json";
"X-Auth-Key"=$ApiKey;
"X-Auth-Email"=$Email;
}
$zone = Invoke-WebRequest -UseBasicParsing -Method GET -Headers $headers -Uri "https://api.cloudflare.com/client/v4/zones/" | ConvertFrom-Json | select -ExpandProperty result | ? { $_.name -eq $Domain }
$zoneId = $zone.id
$recordName = "$Name.$Domain"
$record = Invoke-WebRequest -UseBasicParsing -Method GET -Headers $headers -Uri "https://api.cloudflare.com/client/v4/zones/$zoneId/dns_records" | ConvertFrom-Json | select -ExpandProperty result | ? { $_.name -eq $recordName }
$publicIP = Invoke-RestMethod http://ipinfo.io/json | select -ExpandProperty ip
if ($record) {
Write-Output "Updating DNS record"
$recordId = $record.id
$body = @{
"type" = "A";
"name" = $Name;
"content" = $publicIP;
"proxied" = $false
}
$json = ConvertTo-Json $body
Invoke-WebRequest -UseBasicParsing -Method PUT -Headers $headers -Body $json -Uri "https://api.cloudflare.com/client/v4/zones/$zoneId/dns_records/$recordid" | ConvertFrom-Json | select -ExpandProperty result
} else {
Write-Output "Creating new DNS record"
$body = @{
"type" = "A";
"name" = $Name;
"content" = $publicIP;
"proxied" = $false
}
$json = ConvertTo-Json $body
Invoke-WebRequest -UseBasicParsing -Method POST -Headers $headers -Body $json -Uri "https://api.cloudflare.com/client/v4/zones/$zoneId/dns_records" | ConvertFrom-Json | select -ExpandProperty result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment