Windows 10 个脚本到 enable/disable TCP 端口

Windows 10 script to enable/disable TCP port

我想为我的应用程序模拟 Oracle 断开连接。我需要 enable/disable 快速将流量传出到端口 1521。

我认为最好的方法是 add/remove 一条规则进入 Windows 防火墙。

有我可以使用的 CMD 或 PowerShell 脚本吗?还是 C# 中的 WMI?

(注意:我不能整体禁用网络,因为我的代码中使用了不同的连接,只需要禁用 1521)。

试试这个,这个命令将阻止来自端口 1521 的任何应用或工具的任何出站流量:

New-NetFirewallRule -DisplayName "Disabling Port 1521" -Action Block -Direction Outbound -DynamicTarget Any -EdgeTraversalPolicy Block -Profile Any -Protocol tcp -RemotePort 1521

感谢 Anas Atef,我不得不卷起袖子。以下是像我这样的情况需要的东西:

New-NetFirewallRule -DisplayName "Disabling Port 1521" -Action Block -Direction Outbound -Profile Any -Protocol tcp -RemotePort 1521
Set-NetFirewallRule -DisplayName "Disabling Port 1521" -Action Allow
Set-NetFirewallRule -DisplayName "Disabling Port 1521" -Action Block
Remove-NetFirewallRule -Name "{751a86cb-2ef3-4eba-8c95-68aa7e4bde18}"

New 创建规则。显示名称是您喜欢的任何名称。

设置即可有效帮助您enable/disable规则。

然后在完成测试后使用删除。 Remove 命令中的 Name 是您在 运行 New 之后获得的 guid(观察输出)。