How to use dynamic dns services in your windows firewall rules.

It is sometimes very useful to be able to create rules in your windows firewall configuration using dynamic dns FQDN instead of simple ip addresses. One nice way to address this is to create a PowerShell script that will run in the background and do this for you.

Follow these simple steps to create your script by using simple PowerShell commands. Then simply create a scheduled task to run it in predetermined intervals and update your rules.

  • Create a PowerShell script and name it fwupdate.ps1 or any name of your choice.
  • Add the following lines to your script replacing “fqdn_name” with your Dynamic IP FQDN address and “fwrule” with the name of the associated firewall rule.
  • $ip = [System.Net.Dns]::GetHostAddresses(“fqdn_name”).IpAddressToString
  • Set-NetFirewallRule -DisplayName “fwrule” -RemoteAddress $ip
  • Create a firewall rule and define ports etc. before hand and name it either “fwrule” and use the second line as it is ,or change the “fwrule” to your rule’s display name on advanced firewall app on windows server.
  • Save your script to a disc location of your choice “path_of_choice” in my example.
  • Then create a scheduled task like the one below to run every 5 mins or so to update your firewall rule.
  • powershell -executionpolicy remotesigned -File C:\path_of_choice\fwupdate.ps1 >> c:\path_of_choice\firewall\firewall.log

That’s it your firewall rule will now update according to the new dynamic IP address extremely helpful and convenient.

Hope you liked this tip.