5

I am using a script to set firewall rules in netsh.

netsh.exe advfirewall firewall set rule name="Block External IP's" new remoteip="$($ipArray[0])"

I thought that set was used so that it would append the rule rather than replace it. However, it wrote over the previous ip's in the list and replaced it with the very last ip in the array. How can I rewrite this so that it adds to the rules rather than replaces them? Thank you so much for your time and help.

1

1 Answer 1

6

I’m not sure but I guess you use PowerShell to loop over the array $IPArray and want to add every IP in this array to be the Remote IP of the firewall rule Block External IP’s.

If this is correct, the problem is that netsh does not have the option to add an IP, any set remoteip= command overwrites the previous entry. Therefore you overwrite the remote IP with every iteration of your loop.

I think the solution is easy: Loop over the array as before, but combine all entries from $IPArray to a string, separated by ,. The string then should look like this 192.168.1.1,52.3.7.8.

Finally, issue one netsh command and specific this string as the remote IP parameter like this (given you name the variable RemoteIPString

netsh.exe advfirewall firewall set rule name="Block External IP's" new remoteip="$RemoteIPString"
2
  • 1
    Thank you. It'd be so much easier if Windows allowed users add to the list rather it being overridden by default. Looks like I have to change my script to write to another file and then have the rule read from it. Seems tortuous.
    – Zombian
    Aug 3, 2012 at 20:23
  • 3
    Only issue with this I'm trying to ban 6 thousand IP's (TorNetwork) and hitting a command line character limit. So the only option would be to figure out howto append to an existing rule.
    – bfritz
    Sep 21, 2014 at 1:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .