gpt4 book ai didi

powershell - 加快Powershell的remove-netfirewallrule的速度

转载 作者:行者123 更新时间:2023-12-03 01:10:09 25 4
gpt4 key购买 nike

我需要使用Powershell在Windows 10中删除许多与Metro应用程序相关的防火墙规则。与netsh或regedit相比,它看起来非常慢。无论如何要加快速度?

# clean firewall rules, deleting profile doesn't get rid of them

# string (sid) 45 in length, no existing profiles
# 9000 rules take about 90 minutes to delete

$profiles = get-wmiobject -class win32_userprofile

# I'm only dumping to a file to convert pscustomobject to string for sort
get-netfirewallrule -all | select-object -property owner > out

$list = get-content out | sort-object | get-unique | where-object { $_.trim().length -eq 45 -and $profiles.sid -notcontains $_ }

foreach($i in $list) {$i
remove-netfirewallrule -owner $i}

# about 65 rules per user here
echo ConfigurableServiceStore
get-netfirewallrule -all -policystore configurableservicestore | select-object -property owner > out

$list = get-content out | sort-object | get-unique | where-object { $_.trim().length -eq 45 -and $profiles.sid -notcontains $_ }

foreach($i in $list) {$i
remove-netfirewallrule -policystore configurableservicestore -owner $i}

最佳答案

编辑:我已经将Select-Object -Property Owner更新为Select-Object -ExpandProperty Owner,这种方式$_仅包含Owner属性:

$SID = (get-wmiobject -class win32_userprofile).SID

Write-Host "Getting Firewall Rules"
$Rules = Get-NetFirewallRule -All | Select-Object -ExpandProperty Owner -Unique | Where-Object { $SID -notcontains $_ }

Write-Host "Getting Firewall Rules from ConfigurableServiceStore Store"
$ConfigurableServiceStore = Get-NetFirewallRule -All -PolicyStore ConfigurableServiceStore | Select-Object -ExpandProperty Owner -Unique | Where-Object { $SID -notcontains $_ }

Write-Host "Deleting Firewall Rules:" -ForegroundColor Green
foreach($Owner in $Rules) {
Write-Host "Deleting Rules with Owner: $Owner"
Remove-NetFirewallRule -Owner $Owner
}

Write-Host "Deleting Firewall Rules from ConfigurableServiceStore Store:" -ForegroundColor Green
foreach($Rule in $ConfigurableServiceStore) {
Write-Host "Deleting Rules with Owner: $Owner"
Remove-NetFirewallRule -PolicyStore ConfigurableServiceStore -Owner $Owner
}

关于powershell - 加快Powershell的remove-netfirewallrule的速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40620634/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com