Update FreeDNS with PowerShell and Task Scheduler

After my long loved Raspberry Pi died I needed a new way to update a dynamic DNS. I recently discovered the Invoke-WebRequest cmdlet that lets you send an HTTP(S) request and parse pretty much whatever you get in return. My use for this is to keep a site-to-site VPN to my lab up and running.

# Change Path to desired log location and Uri to your Direct or Token URL from FreeDNS
$LogPath = "C:\Scripts\Update-FreeDNS.log"
$Uri = "http://sync.afraid.org/u/your_token/"

# No need to change this
Add-Content -Path $LogPath -Value "$(Get-Date) $(Invoke-WebRequest -Uri $Uri)"

Your log file will look something like this

11/13/2019 18:00:03 No IP change detected for your.dyn.dns with IP 28.100.14.108, skipping update
11/13/2019 19:00:03 No IP change detected for your.dyn.dns with IP 28.100.14.108, skipping update
11/13/2019 20:00:03 No IP change detected for your.dyn.dns with IP 28.100.14.108, skipping update

Save these files to somewhere that makes sense, for example C:\Scripts.

  • Open Task Scheduler select Task Scheduler Library to the left and click Create Task to the right
  • Name your task “Update-FreeDNS” or something else explaining
  • You have to check “Run whether user is logged on or not” so if you do not want your credentials to be saved, create a new user and change to that
  • On the trigger tab you can create a schedule that suits your needs. I use every hour, but this is totally up to you
  • Under actions click New and paste the following
Program/scripts: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Add arguments: "C:\Scripts\Update-FreeDNS.ps1"
  • At this point you are finished with the necessities, but feel free to click around to see if you need any more options
  • OK out and you will be asked for your password
  • Run the task on demand and see the result in the log file

As always, ask if anything is unclear.

2 comments

  1. If, like me, your Task Scheduler seems to be running the task properly but no log appears, the IP address isn’t updated and it’s driving you crazy, it’s probably due to PowerShell’s Execution Policy. By default Windows 10 has it on “Restricted”.

    “No PowerShell scripts can be run. Windows PowerShell can be used only in interactive mode. This means that you can only run individual commands. You can’t run scripts under this policy, regardless of where the scripts came from (local or downloaded) and whether they are signed.”
    source: https://www.tenforums.com/tutorials/54585-change-powershell-script-execution-policy-windows-10-a.html

    The fix is to bypass the Execution Policy in the task itself. You do this with:
    -ExecutionPolicy Bypass -File “E:\Programs\FreeDNS Update\simple script.ps1”

    I’m not sure how the code is going to show up in this comment, but this is my actual command line in the “Add arguments (optional):” box.

    I want to thank Guy Leech who runs a more advanced script of his own and I figured it out comparing his command line to the one suggested here.
    https://guyrleech.wordpress.com/2016/12/16/script-to-update-dynamic-dns-registrations/

Leave a Reply to frenchdna Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.