Вы должны ожидать, что это займет время, так как вы просите portfilter проверить все свои правила на соответствие правилам брандмауэра, который просто проверяет все правила на себя.
Пример:
Я не знаю, с какими правилами вы имеете дело, но в моей автономной системе:
($ total = (Get-NetFirewallRule) .count)
783
($ portfilter = Get-NetFirewallPortFilter) .Count
783
Это означает, что код выполняется 1566 раз (в моей системе), потому что вы запрашиваете фильтр у каждого из его собственных правил против всех правил 783 portfilter против правил брандмауэра 783 для создания вашего объекта. ForLoops просто медленные, и с 1566 проходами, в моем случае, хорошо, вы должны собрать, сколько это будет складывать.
Если вы сделали это только для одного правила брандмауэра, вы получите что-то вроде:
Measure-Command {
$RuleCount = 0
$testcount = 0
($total = (Get-NetFirewallRule).count)
($portfilter = Get-NetFirewallPortFilter).Count
ForEach($Rule in (Get-NetFirewallRule | Select -First 1))
{
$portfilter = Get-NetFirewallPortFilter |
ForEach-Object{
$testcount++
$testcount
[pscustomobject]@{
DisplayName = $Rule.DisplayName
Profile = $Rule.Profile
Action = $Rule.Action
Direction = $Rule.Direction
Protocol = $_.Protocol
LocalPort = $_.LocalPort
RemotePort = $_.RemotePort
IcmpType = $_.IcmpType
DynamicTarget = $_.DynamicTarget
}
return
}
$RuleCount++
$perc=[Int]($RuleCount/$total*100)
Write-Progress -Activity 'My Important Activityssss' -PercentComplete $perc -Status $perc
}
}
Days : 0
Hours : 0
Minutes : 0
Seconds : 2 ********* * times the total needed passes
Milliseconds : 414
Ticks : 24149617
TotalDays : 2.79509456018519E-05
TotalHours : 0.000670822694444444
TotalMinutes : 0.0402493616666667
TotalSeconds : 2.4149617 **************
TotalMilliseconds : 2414.9617
Если мы немного доработаем ваш код, чтобы показать больше информации / прогресса, скажем так ...
Clear-Host
$total = (Get-NetFirewallRule).count
$total1 = (Get-NetFirewallPortFilter).Count
$RuleCount = 0
ForEach($Rule in (Get-NetFirewallRule | Select -First 3))
{
Write-host "Processing firewall rule $($Rule.Name)" -ForegroundColor Cyan
$RuleCount++
$perc = [Int]($RuleCount/$total*100)
Write-Progress -Activity 'My Important firewall rules' -PercentComplete $perc -Status $perc -Id 1
$testcount = 0
Get-NetFirewallPortFilter |
ForEach-Object {
Write-host "Processing port rule $($_.Name)" -ForegroundColor Yellow
$testcount++
$perc1 = [Int]($testcount/$total1*100)
Write-Progress -Activity 'My Important Port rules' -PercentComplete $perc1 -Status $perc1 -Id 2
}
Write-Warning -Message "$testcount "
}
Processing firewall rule vm-monitoring-dcom
Processing port rule
...
WARNING: 783
Processing firewall rule vm-monitoring-icmpv4
Processing port rule
...
WARNING: 783
Processing firewall rule vm-monitoring-icmpv6
Processing port rule
...
WARNING: 783
... это должно дополнительно проиллюстрировать то, что я пытаюсь сказать.
Тогда у вас есть ограничения самой системы, скорости процессора, ресурсов памяти / скорости, любых других процессов, которые вы запускаете на своей машине.
См. Аналогичное обсуждение вопросов и ответов:
Как ускорить PowerShell, чтобы получить правила брандмауэра для Windows 10?
#Using a registry approach
param
(
[switch]$Local,
[switch]$GPO
)
# If no switches are set the script will default to local firewall rules
if (!($Local) -and !($Gpo))
{ $Local = $true }
$RegistryKeys = @()
if ($Local) {$RegistryKeys += 'Registry::HKLM\System\CurrentControlSet\Services\SharedAccess\Parameters\FirewallPolicy\FirewallRules'}
if ($GPO) {$RegistryKeys += 'Registry::HKLM\Software\Policies\Microsoft\WindowsFirewall\FirewallRules'}
Foreach ($Key in $RegistryKeys)
{
if (Test-Path -Path $Key)
{
(Get-ItemProperty -Path $Key).PSObject.Members |
Where-Object {
(@('PSPath','PSParentPath','PSChildName') -notcontains $_.Name) -and
($_.MemberType -eq 'NoteProperty') -and
($_.TypeNameOfValue -eq 'System.String')} |
ForEach-Object {
# Prepare hashtable
$HashProps = @{
NameOfRule = $_.Name
RuleVersion = ($_.Value -split '\|')[0]
Action = $null
Active = $null
Dir = $null
Protocol = $null
LPort = $null
App = $null
Name = $null
Desc = $null
EmbedCtxt = $null
Profile = $null
RA4 = $null
RA6 = $null
Svc = $null
RPort = $null
ICMP6 = $null
Edge = $null
LA4 = $null
LA6 = $null
ICMP4 = $null
LPort2_10 = $null
RPort2_10 = $null
}
# Determine if this is a local or a group policy rule and display this in the hashtable
if ($Key -match 'HKLM\\System\\CurrentControlSet')
{ $HashProps.RuleType = 'Local' }
else
{ $HashProps.RuleType = 'GPO' }
# Iterate through the value of the registry key and fill PSObject with the relevant data
ForEach ($FireWallRule in ($_.Value -split '\|'))
{
switch (($FireWallRule -split '=')[0])
{
'Action' {$HashProps.Action = ($FireWallRule -split '=')[1]}
'Active' {$HashProps.Active = ($FireWallRule -split '=')[1]}
'Dir' {$HashProps.Dir = ($FireWallRule -split '=')[1]}
'Protocol' {$HashProps.Protocol = ($FireWallRule -split '=')[1]}
'LPort' {$HashProps.LPort = ($FireWallRule -split '=')[1]}
'App' {$HashProps.App = ($FireWallRule -split '=')[1]}
'Name' {$HashProps.Name = ($FireWallRule -split '=')[1]}
'Desc' {$HashProps.Desc = ($FireWallRule -split '=')[1]}
'EmbedCtxt' {$HashProps.EmbedCtxt = ($FireWallRule -split '=')[1]}
'Profile' {$HashProps.Profile = ($FireWallRule -split '=')[1]}
'RA4' {[array]$HashProps.RA4 += ($FireWallRule -split '=')[1]}
'RA6' {[array]$HashProps.RA6 += ($FireWallRule -split '=')[1]}
'Svc' {$HashProps.Svc = ($FireWallRule -split '=')[1]}
'RPort' {$HashProps.RPort = ($FireWallRule -split '=')[1]}
'ICMP6' {$HashProps.ICMP6 = ($FireWallRule -split '=')[1]}
'Edge' {$HashProps.Edge = ($FireWallRule -split '=')[1]}
'LA4' {[array]$HashProps.LA4 += ($FireWallRule -split '=')[1]}
'LA6' {[array]$HashProps.LA6 += ($FireWallRule -split '=')[1]}
'ICMP4' {$HashProps.ICMP4 = ($FireWallRule -split '=')[1]}
'LPort2_10' {$HashProps.LPort2_10 = ($FireWallRule -split '=')[1]}
'RPort2_10' {$HashProps.RPort2_10 = ($FireWallRule -split '=')[1]}
Default {}
}
}
# Create and output object using the properties defined in the hashtable
New-Object -TypeName 'PSCustomObject' -Property $HashProps
}
}
}
и ссылка из этого поста на:
2.2.2.19 Правило брандмауэра и правило грамматики брандмауэра