如何清除 Windows 防火墙中阻止的 IP?

如何清除 Windows 防火墙中阻止的 IP?

我在 Windows 防火墙中设置了阻止规则,在“范围”选项卡下,它包含数百个在某个时间点添加的单独地址。我发现我必须单击“IP 并删除”每个地址。

是否有办法编写脚本或以某种方式清除整个列表?

答案1

尝试一下这个(你可以使用 Powershell ISE):

$ruleName = "NAME" # change name
$addrToDelete= "1.2.3.4" # change address

$rules=Get-NetFirewallRule -Enabled True |?{$_.DisplayName -eq $ruleName}
$allAddr=$rules[0] | Get-NetFirewallAddressFilter | Select-Object -ExpandProperty LocalAddress # LocalAddres or RemoteAddress
$inputObj=$rules[0] | Get-NetFirewallAddressFilter
$delAddr=$allAddr -eq $addrToDelete
$allAddr = $allAddr -replace $delAddr -replace ""
$allAddrAfter = $AllAddr -join ''
Set-NetFirewallAddressFilter -InputObject $inputObj -LocalAddress $allAddrAfter

要删除多个地址:

$ipsToRemove = @("1.2.3.4", "4.3.2.1") # paste addresses that you want to remove
$ruleName = "test" # name of the firewall role
$rules = Get-NetFirewallRule -Enabled True | Where-Object { $_.DisplayName -eq $ruleName }
$inputObj = $rules[0] | Get-NetFirewallAddressFilter 
$allAddr = $inputObj | Select-Object -ExpandProperty LocalAddress # LocalAddresses or RemoteAddresses
$filteredAddresses = $allAddr | Where-Object{ $_ -notin $ipsToRemove }
Set-NetFirewallAddressFilter -InputObject $inputObj -LocalAddress $filteredAddresses # LocalAddresses or RemoteAddresses

此脚本删除了 LocallAdresses - 如果您想删除 RemoteAddresses,您需要在代码的第 5 行和最后一行中进行更改。

相关内容