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:

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.

Chrissy is a PowerShell MVP who has worked in IT for nearly 20 years, and currently serves as a Sr. Database Engineer in Belgium. Always an avid scripter, she attended the Monad session at Microsoft’s Professional Developers Conference in Los Angeles back in 2005 and has worked and played with PowerShell ever since. Chrissy is currently pursuing an MS in Systems Engineering at Regis University and helps maintain RealCajunRecipes.com in her spare time. She holds a number of certifications, including those relating to SQL Server, SuSE Linux, SharePoint and network security. She recently became co-lead of the SQL PASS PowerShell Virtual Chapter. You can follow her on Twitter at @cl.

Posted in Networking, PowerShell

Leave a Reply

Your email address will not be published. Required fields are marked *

*