我如何找到 TCP/UDP 听众? (网络统计)

How do I find TCP/UDP listeners? (netstat)

我试过:

  1. Add-Type -AssemblyName System.Net
  2. Add-Type -Path "C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Net.dll"
  3. [System.Reflection.Assembly]::LoadWithPartialName("system.net")
  4. add-type -path "C:\Windows\assembly\GAC_MSIL\System.Net.5.0.0__b03f5f7f11d50a3a\System.Net.dll"

所有这些似乎都成功了并且没有产生错误。然而,[net](或[System.Net])仍然给出

Unable to find type [net]: make sure that the assembly containing this type is loaded.

有没有办法从 Powershell 中使用这些 类,或者我该如何调用成员?我不知道 system.net 是否真的会得到我想要的东西,但我正在尝试大致了解如何在 Powershell 中使用 .Net 类。


I am trying to get a list of TCP or UDP ports that the machine is listening on to avoid having to create some program to parse netstat output.

根据您的评论:

I am actually trying to get a list of TCP or UDP ports that the machine is listening on to avoid having to create some program to parse netstat output

我找到了 this answer based on C#:

$Properties = [Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties()

$Connections = $Properties.GetActiveTcpConnections()
ForEach ($Conn in $Connections)
{
    "$($Conn.LocalEndPoint) <==> $($Conn.RemoteEndPoint)"
}

并且进一步基于this answer,您可以使用GetActiveTcpListeners()方法找到所有监听器:

$Listeners = $Properties.GetActiveTcpListeners()

More information on this object can be found here.