如何通过InstanceID寻址特定端口来获取防火墙规则的名称

我试图确定所有的防火墙规则,这是处理特定的端口。 这是我可以如何手动做到这一点:

Get-NetFirewallPortFilter | Where { $_.LocalPort -Eq "RPC" } ` | Format-Table -Property InstanceID 

现在我需要知道规则的名称:

 Show-NetFirewallRule | Where { $_.InstanceID –Eq “Netlogon-TCP-RPC-In”} ` | Format-Table -Property DisplayName 

然后我得到:

 DisplayName ----------- Netlogon Service Authz (RPC) 

我不想手动做
我试过下面的脚本来获取名称列表:

 $InstanceIDs = Get-NetFirewallPortFilter | Where { $_.LocalPort -Eq "RPC" } ` | Format-Table -Property InstanceID foreach ($InstanceID in $InstanceIDs) { Show-NetFirewallRule | Where { $_.InstanceID –Eq $InstanceID} ` | Format-Table -Property DisplayName } 

在这一点上什么都没有。
找出$InstanceIDs InstanceID内的$InstanceIDs不是一个string,但没有转换它。

任何想法如何使其工作?
谢谢。

你的问题与$InstanceID types有关 (在脚本中尝试$InstanceID.Gettype() )。 下一个代码片段可以帮助:

 $InstanceIDs = -split $( Get-NetFirewallPortFilter | Where { $_.LocalPort -Eq "RPC" } | Format-Table -Property InstanceID -HideTableHeaders | Out-String ) Show-NetFirewallRule | Where { $_.InstanceID –In $InstanceIDs} | ForEach-Object { if ($_.Name) { "{0} {1} {2}" -f $_.Name, '==', $_.DisplayName } } 

请注意, $InstanceIDs是上面的代码片段中的string数组; 而且, Show-NetFirewallRule中的Get-NetFirewallPortFilter属性对应于Get-NetFirewallPortFilter