Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
I found this which basically says that netsh is not capable of adding ip's. So the only option I would have is to keep a list in a text file and run a script that writes to the file and then reads from it, importing all the addresses? That seems crazy.… – Zombian Aug 3 '12 at 19:51
up vote 4 down vote accepted

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,

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"
share|improve this answer
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 '12 at 20:23
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 '14 at 1:44

You must log in to answer this question.

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