0

I need some help to make an script that check if exist a rule, and add if not. It auto add all exe inside a folder where i run it.

@echo off
cls

Echo.--------------------------------------------------------------------------------------------------------------
FOR /r %%G in ("*.exe") Do (

netsh advfirewall firewall show rule name=all | find "Nombre de regla:" | find "%%G" > nul
if %errorlevel% EQU 1 (
@echo Added - %%G
NETSH advfirewall firewall add rule name="%%G" dir=in program="%%G" action="block" enable="yes" > nul
NETSH advfirewall firewall add rule name="%%G" dir=out program="%%G" action="block" enable="yes" > nul
) else ( 
@echo Exist- %%G 
)
)

Echo.--------------------------------------------------------------------------------------------------------------
Echo.
Echo End.
Echo.
pause

What I doing wrong? Thank you!!!

kapi
  • 1
  • 2

1 Answers1

2

I strongly suggest using PowerShell. There is a specific set of cmdlets for managing Windows Firewall, and it's a lot better environment for scripting.

https://docs.microsoft.com/en-us/powershell/module/netsecurity/get-netfirewallrule https://docs.microsoft.com/en-us/powershell/module/netsecurity/new-netfirewallrule https://docs.microsoft.com/en-us/powershell/module/netsecurity/set-netfirewallrule https://docs.microsoft.com/en-us/powershell/module/netsecurity/remove-netfirewallrule

Massimo
  • 70,200
  • 57
  • 200
  • 323