为什么Get-NetFirewallRule没有显示防火墙规则的所有信息? (如 netsh)

问题描述 投票:0回答:5

我试图查找是否已存在具有相同名称、相同配置的防火墙规则,例如:localport。

所以我使用

Get-NetFirewallRule
列出所有规则,但是返回的规则不包含端口信息,还缺少一些其他信息。在哪里可以找到规则的所有配置。以下是返回的属性:

姓名
显示名称
描述
显示组
团体
启用
轮廓
平台
方向
行动
边缘遍历策略
松散源映射
仅本地映射
所有者
主要状态
地位
执行状态
策略存储源
PolicyStoreSourceType
powershell windows-firewall
5个回答
9
投票

我认为许多人(包括我最近)不理解的是,Get-NetFirewall*Filter 命令提供了搜索防火墙规则的快速快捷方式,就像其他命令中的 -filter 选项一样。如果我这样做,需要很长时间:

Get-NetFirewallRule | Get-NetFirewallPortFilter | 
  Where LocalPort -eq 3389

虽然这几乎是即时的:

Get-NetFirewallPortFilter | Where LocalPort -eq 3389

Get-NetFirewallPortFilter 实际上返回 InstanceID 属性中防火墙规则的名称,默认情况下不显示。这就是为什么您可以将 Get-NetFirewallPortFilter 通过管道返回到 Get-NetFirewallRule。

Get-NetFirewallPortFilter | Where LocalPort -eq 3389 |
  Get-NetFirewallRule

这是一个提供类似于 netsh 的详细输出的函数,其中包含端口、地址和应用程序:

function mynetsh {
  param($displayname)

  $rule = get-netfirewallrule -displayname $displayname
  $address = $rule | Get-NetFirewallAddressFilter
  $port = $rule | Get-NetFirewallPortFilter
  $application = $rule | Get-NetFirewallApplicationFilter
  [pscustomobject]@{
    DisplayName = $rule.DisplayName
    Description = $rule.Description  
    Enabled = $rule.Enabled
    Direction = $rule.Direction
    Profile = $rule.Profile
    DisplayGroup = $rule.DisplayGroup
    LocalAddress = $address.LocalAddress
    RemoteAddress = $address.RemoteAddress
    Protocol = $port.Protocol
    LocalPort = $port.LocalPort
    RemotePort = $port.RemotePort
    EdgeTraversalPolicy = $rule.EdgeTraversalPolicy
    Program = $application.Program 
    Action = $rule.Action
  }
}

mynetsh 'Remote Desktop - User Mode (TCP-In)'

DisplayName         : Remote Desktop - User Mode (TCP-In)
Description         : Inbound rule for the Remote Desktop service to allow RDP traffic. [TCP 3389]
Enabled             : False
Direction           : Inbound
Profile             : Any
DisplayGroup        : Remote Desktop
LocalAddress        : Any
RemoteAddress       : Any
Protocol            : TCP
LocalPort           : 3389
RemotePort          : Any
EdgeTraversalPolicy : Block
Program             : %SystemRoot%\system32\svchost.exe
Action              : Allow

4
投票

为了查找防火墙规则中已有的端口号,您可以使用不同的 cmdlet

Get-NetFirewallPortFilter

信息

使用

Get-NetFirewallRule
过滤您要查看的规则子集并将其通过管道传输到上述 cmdlet。例如:

Get-NetFirewallRule -DisplayName "SQL Broker Service" | Get-NetFirewallPortFilter

听起来您要找的房产是 localport。


2
投票

使用以下命令列出所有内容。

Get-NetFirewallRule| Where { $_.Enabled -eq $True } |
Format-Table -Property Name,
DisplayName,
DisplayGroup,
@{Name='Protocol';Expression={($PSItem | Get-NetFirewallPortFilter).Protocol}},
@{Name='LocalPort';Expression={($PSItem | Get-NetFirewallPortFilter).LocalPort}},
@{Name='RemotePort';Expression={($PSItem | Get-NetFirewallPortFilter).RemotePort}},
@{Name='RemoteAddress';Expression={($PSItem | Get-NetFirewallAddressFilter).RemoteAddress}},
Enabled,
Profile,
Direction,
Action

输出如下所示


0
投票

使用 Select-Object Cmdlet 显示所有属性

这将仅显示第一个,这样您就不会被文本淹没,请根据需要随意删除“-First 1”

Get-NetFirewallRule | select -First 1 -Property *

然而,调查发现似乎没有有关端口的信息,进一步研究它 - 您可能需要使用 Get-NetFirewallPortFilter 并通过 instanceid 匹配它们。如果您需要帮助,我需要更多关于您想要实现的目标的信息。


0
投票

如果您仅使用防火墙 cmdlet 来获取包括端口、程序等的对象列表,这不是一件容易的任务,而且速度非常慢!为什么不尝试传统的方法,即

netsh advfirewall firewall
命令套件。下面是我尝试获取包含所有规则信息的对象列表。

$output = (netsh advfirewall firewall show rule name=all verbose | Out-String).Trim() -split '\r?\n\s*\r?\n'
$propertyNames = [System.Collections.Generic.List[string]]::new()

$objects = @( $(foreach($section in $output ) {
    $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]$_
})
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.