为什么 Get-NetFirewallRule 不显示防火墙规则的所有信息?

dav*_*hou 4 powershell windows-firewall

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

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

姓名
显示名称
描述
显示组
团体
启用
轮廓
平台
方向
行动
边缘遍历策略
松散源映射
仅本地映射
所有者
主要状态
地位
执行状态
策略存储源
策略存储源类型

js2*_*010 9

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

Get-NetFirewallRule | Get-NetFirewallPortFilter | 
  Where LocalPort -eq 3389
Run Code Online (Sandbox Code Playgroud)

虽然这几乎是即时的:

Get-NetFirewallPortFilter | Where LocalPort -eq 3389
Run Code Online (Sandbox Code Playgroud)

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

Get-NetFirewallPortFilter | Where LocalPort -eq 3389 |
  Get-NetFirewallRule
Run Code Online (Sandbox Code Playgroud)

这是一个提供类似于 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
Run Code Online (Sandbox Code Playgroud)

  • 希望我能不止一次对此投赞成票。感谢您提供清晰的信息、代码示例和该主题的覆盖范围。优秀的帖子。 (2认同)