使用命令提示符修改 Windows 防火墙

modify Windows Firewall with command prompt

使用命令提示符修改Windows防火墙

我可以允许 Domain/Public/Private 复选标记全部出现在同一行中吗(就像我可以手动单击一样)?

到目前为止我有 3 个条目,每种类型有一个复选标记:

netsh advfirewall firewall add rule name="MyApp" dir=in action=allow program="C:\MyApp.exe" enable=yes profile=domain

netsh advfirewall firewall add rule name="MyApp" dir=in action=allow program="C:\MyApp.exe" enable=yes profile=public

netsh advfirewall firewall add rule name="MyApp" dir=in action=allow program="C:\MyApp.exe" enable=yes profile=private

试试这个:

@echo off
setlocal

set "fcmd=netsh advfirewall firewall add rule name="MyApp" dir=in action=allow program="C:\MyApp.exe" enable=yes profile="
for %a in (domain public private) do (
    echo %fcmd%%a

如果您想 运行 它在批处理文件中,只需将 %a 的 % 加倍到 %%a。 测试可接受的输出时删除 Echo。

稍微整理一下:

@echo off
setlocal

set fcmd=netsh advfirewall firewall add rule ^
 name="MyApp" dir=in action=allow program="C:\MyApp.exe" ^
 enable=yes profile=
for %a in (domain public private) do (
    echo %fcmd%%a
)

解决方法如下:

profile=Private and/or public and/or domain
(To add rule in more than one profile use “,” E.g.: profile=private, domain )