我有一个 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
好像: