Powershell: list members of a .net component

16,151

Solution 1

you can use the powershell cmdlet get-member

PS>[system.net.webclient]|get-member  -MemberType method                                                                              


   TypeName : System.RuntimeType                                                                                

Name                           MemberType Definition                                                            
----                           ---------- ----------                                                            
AsType                         Method     type AsType()                                                         
Clone                          Method     System.Object Clone(), System.Object ICloneable.Clone()               

...

we can see there is a GetMethods method, so try :

[system.net.webclient].GetMethods()    

Solution 2

While the other answer is technically correct, they don't answer your question of why the first part of your script is not working.

I think it's a two part reason:

  1. If the assembly is already loaded, it will probably result in a null-value response
  2. The syntax for the LoadWithPartialName method don't use the square brackets, so you need to filter those out of the string, or don't supply them in the first place

You can check if the assembly is already loaded with the following:

[Reflection.Assembly]::GetAssembly('assemblyName')

Wrap it in a try-catch to handle the error if the assembly isn't loaded/found.

Share:
16,151
Marc
Author by

Marc

Updated on June 15, 2022

Comments

  • Marc
    Marc almost 2 years

    I'm trying to build a script that will provide me the listing of the method of a dll from a .net component.

    Here's what I've done so far:

    Param([String]$path)
    if($path.StartsWith("["))
    {
    $asm = [Reflection.Assembly]::LoadWithPartialName($path)
    $asm.GetTypes() | select Name, Namespace | sort Namespace | ft -groupby Namespace
    }
    else
    {
    $asm = [Reflection.Assembly]::LoadFile($path)
    $asm.GetTypes() | select Name, Namespace | sort Namespace | ft -groupby Namespace
    }
    

    So basically the second part of the script (when providing the path of a dll) is working perfectly fine;

    Running '.\GetDllMethods.ps1 -path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"' will provide me with all the members of the WinSCP dll.

    What I want to achieve with the first part of the script is to get the same result by providing the name of the .net component using something like:

    .\GetDllMethods.ps1 -path "[System.IO.StreamWriter]"

    To get all the member of the StreamWriter component.

    But I'm getting a null exception here.. Any hint ?