Use PowerShell and CloudFlare API v4 to Dynamically Update CloudFlare DNS to your External IP

Got a homelab and miss how DynDNS used to be free? Using PowerShell and a CloudFlare hosted domain, you can emulate similar functionality for free using PowerShell and a Scheduled Task. If you're looking for a DDNS client that you can run on a router, check out CloudFlare's ddclient or Scott Helme's post Replacing DynDNS with CloudFlare DDNS.

Don't use CloudFlare? You should consider it. I love CloudFlare because they provide free HTTPS and the DNS management UX is awesome. They're also a CDN, all free. I can't say one bad thing about the service. It's a reverse proxy that works well, and nowadays, any new domain I get goes straight on it, even bepug.be, the Belgian PowerShell User Group :)

Introducing CloudFlareDynDns PowerShell Module

So last week, I created a PowerShell module, CloudFlareDynDns that uses the CloudFlare API to update a specified host name. The basic syntax for this module is:

1Import-Module .\\CloudFlareDynDns.psm1
2Update-CloudFlareDynamicDns -Token 1234567893feefc5f0q5000bfo0c38d90bbeb -Email [email protected] -Zone example.com -Record homelab

cfupdate

(As of 22 Dec 2015, you can find your API key at: https://www.cloudflare.com/a/account/my-account -> API Key)

When Update-CloudFlareDynamicDns is executed, it'll go out to ipinfo.io to get your External IP, then it'll compare that to the current record at CloudFlare. If you use the -UseDns switch, it'll check DNS instead of querying CloudFlare directly. If the record doesn't exist, it'll add it. If it needs to be updated, it'll update it.

When making this a scheduled task, ensure the module is located in your path and successfully loads when you first open PowerShell. Then "Run whether user is logged on or not", store the password, then for the Action, Start a Program -> powershell.exe and set the arguments to the full Update-CloudFlareDynamicDns command. Set to run every 5 minutes indefinitely.

Thanks to Jorrit Salverda and Posh-CloudFlare for getting me started. If you'd like to see a simplified breakdown of the code, see PowerShell Invoke-RestMethod / CloudFlare API v4 Code Sample.