$obj = @{}
foreach( $line in ($section -split '\r?\n') ) {
if( $line -match '^\-+$' ) { continue }
$name, $value = $line -split ':\s*', 2
$name = $name -replace " ", ""
$obj.$name = $value
if($propertyNames -notcontains $name) {
$propertyNames.Add( $name )
}
}
$obj
}) | % {
foreach( $prop in $propertyNames ) {
if( $_.Keys -notcontains $prop ) {
$_.$prop = $null
}
}
[PSCustomObject、$_
})
$r = $objects | Group-Object -Property RuleName, Program, Action, Profiles, RemoteIP, RemotePort, LocalIP, LocalPort, Enabled, Protocol, Direction
# If you want to take a look
# $r | ?{$_.Count -gt 1} | Select-Object -ExpandProperty group | Out-GridView
$r | ?{$_.Count -gt 1} | %{
$name = $_ | Select-Object -ExpandProperty group | Select-Object -ExpandProperty RuleName -First 1
# Here we have to use this cmdlet, since `netsh advfirewall firewall delete` can't differentiate rules with the same names and will delte them all!
Get-NetFirewallRule -DisplayName $name | Select-Object -Skip 1 | Remove-NetFirewallRule
}
将文件另存为 firewall.ps1。
接下来是以管理员身份打开 Windows PowerShell,然后将 firewall.ps1 文件的文件夹路径粘贴为命令提示符。它是这样的:cd C:\Users\
最后,键入脚本标题,即 firewall.ps1,然后按 Enter。
该脚本将开始运行并删除所有重复的 Windows 防火墙规则。
注意:Reddit上还有另一个脚本,你可以看看。
# firewall.ps1
# Get-NetFirewallRule >> x.txt to see what the empty paths are referencing.
ForEach($rule In Get-NetFirewallRule | Get-NetFirewallApplicationFilter) {
If(-not $rule.AppPath) { # UWP apps have no paths.
If($rule.CreationClassName.Substring(0, 22) -ne 'MSFT|FW|FirewallRule|{') { # Crude check for Windows 10 apps.
Write-Host 'REMOVING ID: ' $rule.InstanceID
Get-NetFirewallRule $rule.InstanceID | Remove-NetFirewallRule
#} else {
#Write-Host $rule.CreationClassName
#Get-NetFirewallRule $rule.InstanceID | Format-List Platform
}
} else { # Firewall rule contains a path.
If(-not (Test-Path $rule.AppPath) -and $rule.AppPath -ne 'System') { # Crude check for Windows 10 itself.
Write-Host 'REMOVING APP:' $rule.AppPath
Get-NetFirewallRule $rule.InstanceID | Remove-NetFirewallRule
}
}
}
删除某些规则可能会影响您的网络和相关配置。请谨慎执行这些步骤。有些规则可能看起来相同,但略有不同。如果您知道自己在做什么,请删除它们。
我们希望您能在这里找到有价值的东西。
如何在 Windows 中编写防火墙规则?
若要在 Windows 中编写或创建防火墙规则,请在 Windows 安全中心应用中打开“高级安全 Windows 防火墙”选项,然后选择“入站规则”或“出站规则”。单击“操作”,然后选择“新建规则”。在“规则类型”部分,选择“自定义”,然后选择“下一步”。按照屏幕上的说明按照您想要的方式自定义您的规则。
如何禁用 Windows 防火墙规则
若要禁用 Windows 防火墙规则,请转到 Windows 安全中心应用,然后选择“防火墙和网络保护”。在底部,单击“高级设置”以打开具有高级安全性的Windows Defender防火墙。在此处,选择“入站规则”或“出站规则”。您将看到一长串规则。搜索要禁用的那个,单击它,然后在左侧窗格中选择“禁用规则”。在某些情况下,禁用规则可能比删除规则更好。
相关文章
网友评论(共有 0 条评论)