Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 如何找到TCP/UDP侦听器?(netstat)_.net_Powershell_Powershell 2.0_Windows Server 2008 R2 - Fatal编程技术网

.net 如何找到TCP/UDP侦听器?(netstat)

.net 如何找到TCP/UDP侦听器?(netstat),.net,powershell,powershell-2.0,windows-server-2008-r2,.net,Powershell,Powershell 2.0,Windows Server 2008 R2,我试着做: 添加类型-AssemblyName System.Net 添加类型-路径“C:\Program Files\Reference Assembly\Microsoft\Framework\v3.5\System.Net.dll” [System.Reflection.Assembly]:LoadWithPartialName(“System.net”) 添加类型-路径“C:\Windows\assembly\GAC\u MSIL\System.Net\3.5.0.0\uu b03f5f

我试着做:

  • 添加类型-AssemblyName System.Net
  • 添加类型-路径“C:\Program Files\Reference Assembly\Microsoft\Framework\v3.5\System.Net.dll”
  • [System.Reflection.Assembly]:LoadWithPartialName(“System.net”)
  • 添加类型-路径“C:\Windows\assembly\GAC\u MSIL\System.Net\3.5.0.0\uu b03f5f7f11d50a3a\System.Net.dll”
  • 所有这些似乎都成功了,并且不会产生错误。然而,
    [net]
    (或
    [System.net]
    )仍然提供

    找不到类型[net]:请确保已加载包含此类型的程序集

    是否无法使用Powershell中的这些类,或者如何调用成员?我不知道system.net是否真的会有我想要的东西,但我正试图弄清楚如何在Powershell中使用.net类


    我试图获取计算机正在侦听的TCP或UDP端口的列表,以避免必须创建一些程序来解析netstat输出

    根据您的评论:

    实际上,我正在尝试获取计算机正在侦听的TCP或UDP端口的列表,以避免必须创建一些程序来解析netstat输出

    我发现:


    在ISE或控制台中,键入
    [System.Net.
    和制表符应自动完成各种选项。如果在ISE中执行此操作,则更容易查看可用的方法和子名称空间。关于如何在powershell中使用.Net类,有很多阅读资料。请使用powershell ex:
    添加类型-AssemblyName System.ServiceProcess$sc=$sc=“System.ServiceProcess.ServiceController”-作为[type][reflection.assembly]::GetAssembly($sc)| Get Member
    您尝试访问的是什么类型?您是否尝试过
    [reflection.assembly]::LoadFrom(“C:\Windows\assembly\GAC\U MSIL\System.Net\3.5.0.0\UUU b03f5f7f11d50a3a\System.Net.dll”)
    ?如果API成功加载程序集,它将返回一个
    程序集
    对象,否则它将返回null。您实际要访问的类是什么?
    [System.Net]
    是一个名称空间。将其视为一个容器。它本身不是一个对象,而是一个类型。它是一个类型的容器。@现在我明白了。我没有看到名称空间(也是多部分层次结构)之间的区别和类。在MSDN文档页面的顶部,它指出它是一个名称空间还是一个类。:System.Net.NetworkInformation namespace“以及在其中可以找到方法和属性的“IPGlobalProperties类”下。在PowerShell中[namespace.namespace.class]::Method()或[namespace.namespace.class]::Property。
    $Properties = [Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties()
    
    $Connections = $Properties.GetActiveTcpConnections()
    ForEach ($Conn in $Connections)
    {
        "$($Conn.LocalEndPoint) <==> $($Conn.RemoteEndPoint)"
    }
    
    $Listeners = $Properties.GetActiveTcpListeners()