我需要一些帮助来编写一个脚本,检查是否存在规则,如果不存在则添加。它会自动将所有 exe 添加到我运行它的文件夹中。
@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
我做错了什么?谢谢!!!
答案1
我强烈建议使用 PowerShell。它有一组专门用于管理 Windows 防火墙的 cmdlet,并且它为脚本编写提供了更好的环境。
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