简体   繁体   English

在powershell中删除和应用防火墙规则非常缓慢

[英]Deleting and applying firewall rules in powershell painfully slow

I have several scripts containing New-NetFirewallRule commandlets, in total there are about 200 rules, and to delete existing rules and then apply new ones it takes about 15 minutes.我有几个包含New-NetFirewallRule commandlet 的脚本,总共有大约 200 条规则,删除现有规则然后应用新规则大约需要 15 分钟。

According to documentation New-NetFirewallRule cmdlet has GPOSession paramter which lets you specify GPO object, the GPO is loaded using Open-NetGPO , you do modifications and then save the GPO back.根据文档New-NetFirewallRule cmdlet 具有GPOSession参数,它允许您指定 GPO 对象,使用Open-NetGPO加载 GPO,您进行修改然后将 GPO 保存回来。

However it's useless parameter since it looks like this only works for domains, I'm on Windows 10 single computer network, no domain.但是它是无用的参数,因为它看起来只适用于域,我在 Windows 10 单计算机网络上,没有域。

Obviously each New-NetFirewallRule cmdlet is called independently against localhost GPO, which is too slow, is there a way to load local group policy somehow, do what you want, and then apply changes back without accessing the GPO for each firewall rule?显然,每个New-NetFirewallRule cmdlet 都是针对 localhost GPO 独立调用的,这太慢了,有没有办法以某种方式加载本地组策略,做你想做的事情,然后在不访问每个防火墙规则的 GPO 的情况下应用更改?

EDIT: To respond on comment I think all the rules are simple enough, ie.编辑:为了回应评论,我认为所有规则都很简单,即。 not dealing with heavy calls, like this: (there are 200+ such rules)不处理繁重的电话,像这样:(有 200 多个这样的规则)

New-NetFirewallRule -Confirm:$Execute -Whatif:$Debug -ErrorAction $OnError -Platform $Platform `
-DisplayName "Multicast Domain Name System" -Service Dnscache -Program $ServiceHost `
-PolicyStore $PolicyStore -Enabled True -Action Allow -Group $Group -Profile Private, Domain -InterfaceType $Interface `
-Direction $Direction -Protocol UDP -LocalAddress Any -RemoteAddress 224.0.0.251 -LocalPort 5353 -RemotePort 5353 `
-LocalUser Any -LocalOnlyMapping $false -LooseSourceMapping $false `
-Description "In computer networking, the multicast DNS (mDNS) protocol resolves hostnames to IP addresses
within small networks that do not include a local name server."

A lot of variables are predefined, but only a few variables use more complex call but this happens only once during initialization of a script.许多变量是预定义的,但只有少数变量使用更复杂的调用,但这只会在脚本初始化期间发生一次。

for example to figure out loopback interface alias:例如找出环回接口别名:

$Loopback = Get-NetIPInterface | Where-Object {$_.InterfaceAlias -like "*Loopback*" -and $_.AddressFamily -eq "IPv4"} | Select-Object -ExpandProperty InterfaceAlias

To delete rules, I use this command, which deletes a group of rules:要删除规则,我使用这个命令,它删除一组规则:

Remove-NetFirewallRule -PolicyStore $PolicyStore -Group $Group -Direction $Direction -ErrorAction SilentlyContinue

You could user Local Security Policy tool ( secpol.msc ) to make your configuration and export it to an .inf file.您可以使用本地安全策略工具 ( secpol.msc ) 进行配置并将其导出到.inf文件。 Then on the target machine either use the same tool to import the .inf or secedit.exe for scripting: (if the file exported was mypolicy.inf )然后在目标机器上使用相同的工具导入.infsecedit.exe进行脚本编写:(如果导出的文件是mypolicy.inf

secedit /configure /db %windir%\security\local.sdb /cfg mypolicy.inf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM