You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让netstat输出的表头在Out-GridView中正常显示?

解决PowerShell中netstat输出无法在Out-GridView拆分列的问题

因为netstat是传统外部命令,输出的是无结构纯文本,Out-GridView只能识别PowerShell对象的属性作为列,所以直接管道传递会把整行内容合并成单个字符串列。可以通过两种方式解决:

方式一:解析netstat输出为结构化对象

使用正则提取netstat输出的各字段,转换成自定义对象后再传入Out-GridView:

netstat -f | Select-String '^\s*(TCP|UDP)' | ForEach-Object {
    # 统一空格格式,去除首尾空格
    $cleanLine = $_.Line -replace '\s+', ' ' -replace '^\s+', ''
    # 拆分字段(最多拆分为5部分,适配TCP/UDP的不同输出格式)
    $fields = $cleanLine -split ' ', 5
    [PSCustomObject]@{
        Proto           = $fields[0]
        'Local Address' = $fields[1]
        'Foreign Address' = $fields[2]
        State           = if ($fields[0] -eq 'TCP') { $fields[3] } else { $null }
        PID             = $fields[-1]
    }
} | Out-GridView

方式二:使用PowerShell原生网络命令

推荐使用PowerShell自带的结构化网络命令,无需解析文本,直接输出可被Out-GridView识别的对象:

  • 查看TCP连接:
Get-NetTCPConnection | Select-Object LocalAddress, LocalPort, RemoteAddress, RemotePort, State, OwningProcess | Out-GridView
  • 查看UDP端点:
Get-NetUDPEndpoint | Select-Object LocalAddress, LocalPort, OwningProcess | Out-GridView

内容的提问来源于stack exchange,提问作者user20225877

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 12:15:34