7

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 ?

2 Answers 2

6

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()    
Sign up to request clarification or add additional context in comments.

4 Comments

Ok, I didn't pay attention there was a GetMethods method. It's kinda easing my job. Thanks for the help.
While "[System.IO.StreamWriter].GetMethods() | select Name | sort-object name | get-unique -asstring" will work, I have trouble transfering it into a paramatrized script with the line "[$assembly].GetMethods() | select Name | sort-object name | get-unique -asstring". How can I call the get method on the assembly from its name stored in a string ?
I would use this : $class="system.net.webclient" ; $o=new-object $class ; $o |get-member -MemberType method
It works for your example with "System.Net.WebClient", but it doesn't seem universal because trying with some other .net components such as System.Data.DataTable(example among many) will raise an error... Even though the command "[System.Data.DataTable].GetMethods()" will bring results.
3

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.

4 Comments

Thanks for the hint on the null value if the assembly is not loaded.
While "[System.IO.StreamWriter].GetMethods() | select Name | sort-object name | get-unique -asstring" will work, I have trouble transfering it into a paramatrized script with the line "[$assembly].GetMethods() | select Name | sort-object name | get-unique -asstring". How can I call the get method on the assembly from its name stored in a string ?
I think you need to do it like this: $asm = [System.IO.StreamWriter], then $asm.GetMethods()
Well that would do it if I know in advance the name of the assembly ('System.IO.StreamWriter' was an example), but how to do it dynamically; I want to be able to send as a parameter the name of the assembly to get the methods.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.