how can you enable a windows firewall rule for a dynamic dns name or dns address

in a windows server sometimes you just need to enable some ports on your firewall to your dynamic dns address.(such as RDP , FTP , etc..)
It is cumbersome when you do this manually every time your ip address changes.Why not update your server automatically every time when your ip changes based on your new dynamic dns update ?

1 ) just create a powershell script like this:

$ip = [System.Net.Dns]::GetHostAddresses("your ddnss address").IpAddressToString
Set-NetFirewallRule -DisplayName "myrule" -RemoteAddress $ip

Put in your ddns name like: xxx.dydns.org , no-ip etc..
Create a firewall rule and define ports etc. before hand and name it either "myrule" and use the second line as it is ,or change the "myrule" to your rule's display name on advanced firewall app on windows server.

2) name it as firewall.ps1

3) and than create a task on task scheduler like this:

command to execute:

powershell -executionpolicy remotesigned -File C:\hosting\firewall.ps1 >> c:\hosting\firewall\firewall.log

and set it to run every minute.

thats it. everytime your ip changes and your ddns provider updated with your ip , also your firewall rules will be udpated in a maximum 1 minute of delay.

What script does is:

// Converts your DNS address to IP address and assigns to variable $ip
$ip = [System.Net.Dns]::GetHostAddresses("your ddnss address").IpAddressToString

// Makes your firewall rule named "myrule" to be enabled for your new remote address
Set-NetFirewallRule -DisplayName "myrule" -RemoteAddress $ip

That's it..


Comments

Unknown said…
ÇOK GÜZEL
Unknown said…
BAŞARILAR
http://catiustasiankarada.blogspot.com.tr/
http://tufanbozkurt1.blogspot.com.tr/
http://temizliksirketieryaman.blogspot.com.tr/
http://isiltemizliksirketiankara.blogspot.com.tr/
Unknown said…
This comment has been removed by the author.
Anonymous said…
Thanks for the script!
I got this working perfectly on Windows Server 2012 R2.
I had to get the script to work on Windows 7 as well, but even though I installed Powershell 3.0, it threw an error that Set-NetFirewallRule wasn't recognized.
What I ended up doing instead on that machine was replacing the second line with this:

netsh advfirewall firewall set rule name="myrule" new remoteip=$ip

Which worked perfectly on Windows 7 (I didn't bother testing it on Windows Server 2012 R2 or Windows 8/8.1, but I would assume that it should work as well).
Unknown said…
Thanks for this, solved a problem for me to safely open up a relatives PC for remote support from my dynamic address. I had to employ a little work around to get the script to run, might help someone else. Create a batch file in the same directory as the power shell script with these contents, make sure filetorun is your actual ps script.

=======
@ECHO OFF
SET filetorun=firewallupdate.ps1
SET ThisScriptsDirectory=%~dp0
SET PowerShellScriptPath=%ThisScriptsDirectory%%filetorun%
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& '%PowerShellScriptPath%'";
=======
The the batch file will call the ps file and bypass the control on exection
azharpc said…
I guess I am the only one who came here to share my very own experience. Guess what!? I am using my laptop for almost the past 2 years, but I had no idea of solving some basic issues. I do not know how to saqibtech.net But thankfully, saqibtech.net

virtual safe professional crack
doc converter pro business with crack
windows firewall control crack
movavi photo manager crack
roxio mydvd crack

Popular posts from this blog

Bose speaker hack (companion 5) to turn on/off automatically

how to IR remote control via wemos d1 (esp8266) & mqtt & sonoff tasmota