从 powershell 中的命令输出中选择多个字符串

Selecting multiple strings from the output of command in powershell

这可能是一个简单的问题,之前已经回答过,但我找不到。我正在尝试从我制作的简单脚本中过滤掉不需要的 text/output。

$stop = 2
do {
clear
netstat -a -n -o | Select-String "ESTABLISHED"
Start-Sleep -Seconds 5
} while ($stop -ne 1)

我想添加的不仅仅是已建立的连接到输出 window,例如 UDP 和 TCP 连接,但要删除环回地址。如果有更好的方法或更有效的方法,那就太好了。

由于您使用的是 Windows 10,因此您可以访问 Get-NetTCPConnection cmdlet。您可以使用 objects:

而不是解析 netstat 输出
Get-NetTCPConnection |
    Where-Object RemoteAddress -notin '127.0.0.1','0.0.0.0', '::'

对于 UDP:

Get-NetUDPEndpoint