1

I tried doing:

  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\3.5.0.0__b03f5f7f11d50a3a\System.Net.dll"

All of those appear to succeed and do not produce an error. Yet, [net] (or [System.Net]) still gives

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

Is there no way to use these classes from Powershell, or how do I then call members? I don't know if system.net is actually going to have what I want anyway, but I'm trying to generally figure out how to use .Net classes in Powershell.


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.

Maximilian Burszley
  • 18,243
  • 4
  • 34
  • 63
Roman
  • 344
  • 1
  • 6
  • 19
  • in the ISE or console, type `[System.Net.` and tabbing should autocomplete various options. If you do it in the ISE it's easier to see the available methods and sub namespaces – trebleCode May 02 '18 at 18:31
  • There are plenty of reads on how to use .net classes in powershell. [MS Article](https://blogs.technet.microsoft.com/heyscriptingguy/2010/11/11/use-powershell-to-work-with-the-net-framework-classes/), and [4Sysops Article](https://4sysops.com/wiki/useful-net-classes-for-powershell/). Checking with powershell ex: `Add-Type -AssemblyName System.ServiceProcess $sc = $sc = "System.ServiceProcess.ServiceController" -as [type] [reflection.assembly]::GetAssembly($sc) | Get-Member` – jrider May 02 '18 at 18:41
  • 3
    What type are you trying to access? – Maximilian Burszley May 02 '18 at 18:43
  • 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. So, it doesn't look like system.net has any of that, from browsing through properties and methods. – Roman May 02 '18 at 19:33
  • In ISE, I can type [System.Net.NetworkInformation]. and then it will make suggestions of properties and methods. But, if for example, I try to run [System.Net.NetworkInformation].GetProperties(), I get Unable to find type [System.Net.NetworkInformation]. So, while it will autocomplete, it still doesn't work. – Roman May 02 '18 at 19:35
  • 1
    Have you tried `[reflection.assembly]::LoadFrom("C:\Windows\assembly\GAC_MSIL\System.Net\3.5.0.0__b03f5f7f11d50a3a\System.Net.dll")`? If the API succeeds in loading the assembly, it will return an `Assembly` object, otherwise it returns null. – Bruce Payette May 02 '18 at 20:51
  • 1
    [`[System.Net.NetworkInformation]` is only a namespace.](https://msdn.microsoft.com/en-us/library/system.net.networkinformation(v=vs.110).aspx) What class are you actually trying to access? – Maximilian Burszley May 02 '18 at 21:20

1 Answers1

1

Based on your comment:

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

I found this answer based on C#:

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

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

And further based on this answer, you can find all listeners with the GetActiveTcpListeners() method:

$Listeners = $Properties.GetActiveTcpListeners()

More information on this object can be found here.

Maximilian Burszley
  • 18,243
  • 4
  • 34
  • 63
  • `[System.Net]` is a **_namespace_**. Think of it as a *container*. It itself is not an object, a type. It's a container for types. @Roman – Maximilian Burszley May 02 '18 at 22:10
  • Now I see. I didn't see the difference between namespace (which also is multipart hierarchical) and class. And at the top of the MSDN documentation page, it says whether it is a namespace or class. :System.Net.NetworkInformation Namespace" and under that "IPGlobalProperties Class" wherein one can find methods and properties. In PowerShell [namespace.namespace.class]::Method() or [namespace.namespace.class]::Property. – Roman May 05 '18 at 21:01