通过 PowerShell 对所有入站 Windows Defender 防火墙规则进行输出网格

通过 PowerShell 对所有入站 Windows Defender 防火墙规则进行输出网格

我有一个 PowerShell 脚本,可以运行,但我遇到的问题是获取像 Excel 表这样的输出网格。

以下是代码:

Get-NetFirewallRule |
    Where-Object {($_.Profiles -band [Microsoft.PowerShell.Cmdletization.GeneratedTypes.NetSecurity.Profile]::Domain) -or ($_.Profiles -eq [Microsoft.PowerShell.Cmdletization.GeneratedTypes.NetSecurity.Profile]::Any)} |
    ForEach-Object {
        $portFilter = Get-NetFirewallPortFilter -AssociatedNetFirewallRule $_
        $_ | Select-Object -Property `
            Name,
            DisplayName,
            DisplayGroup,
            Profiles,
            Direction,
            @{n='Protocol'; e={$portFilter.Protocol}},
            @{n='LocalPort'; e={$portFilter.LocalPort}},
            @{n='RemotePort'; e={$portFilter.RemotePort}},
            @{n='RemoteAddress'; e={(Get-NetFirewallAddressFilter -AssociatedNetFirewallRule $_).RemoteAddress}},
            Enabled,
            Profile,
            Action
    }

如何将输出转换为网格格式,以便像 Excel 电子表格一样显示输出。

答案1

只需添加| Out-GridView到末尾:

Get-NetFirewallRule |
    Where-Object {($_.Profiles -band [Microsoft.PowerShell.Cmdletization.GeneratedTypes.NetSecurity.Profile]::Domain) -or ($_.Profiles -eq [Microsoft.PowerShell.Cmdletization.GeneratedTypes.NetSecurity.Profile]::Any)} |
    ForEach-Object {
        $portFilter = Get-NetFirewallPortFilter -AssociatedNetFirewallRule $_
        $_ | Select-Object -Property `
            Name,
            DisplayName,
            DisplayGroup,
            Profiles,
            Direction,
            @{n='Protocol'; e={$portFilter.Protocol}},
            @{n='LocalPort'; e={$portFilter.LocalPort}},
            @{n='RemotePort'; e={$portFilter.RemotePort}},
            @{n='RemoteAddress'; e={(Get-NetFirewallAddressFilter -AssociatedNetFirewallRule $_).RemoteAddress}},
            Enabled,
            Profile,
            Action
    } | Out-GridView

好像:

截屏

相关内容