7

Is there a way to get the parameters for a Method MemberType when using Get-Member?

Example:

Get-Process | Get-Member -MemberType Method

What I need from this example is the parameters and parameter types of each member in the list.

The purpose of this is to get the members, parameters, and parameter types of a COM+ object I have to create documentation for. So an example can't be .net specific.

I will be piping the member and parameter info into a razor template to generate the appropriate html.

Edit: A better example would be this...

$comObj = New-Object -ComObject COMAdmin.COMAdminCatalog
$comObj | Get-Member -MemberType Method

In this example I need to get the parameter names (if there are any) for each method returned.

3 Answers 3

9

Get-Member is meant more for exploring .NET types than PowerShell Commands. For a simple "view" of a command's parameters try this:

Get-Command Get-Process -Syntax

For details on the parameters try this:

Get-Command Get-Process | Select -Expand ParameterSets

If you're looking for more detail on a .NET type member's parameters then try this:

C:\PS> Get-Process | Get-Member -Name WaitForExit

   TypeName: System.Diagnostics.Process

Name        MemberType Definition
----        ---------- ----------
WaitForExit Method     bool WaitForExit(int milliseconds), void WaitForExit()

As for COM objects, that is likely to be a bit more hit or miss. PowerShell doesn't always get type metadata for COM objects. BTW I do get parameter info (the parameter types) for the COM object you list in your question:

C:\PS> $comObj = New-Object -ComObject COMAdmin.COMAdminCatalog
C:\PS> $comObj | gm QueryApplicationFile


   TypeName: System.__ComObject#{790c6e0b-9194-4cc9-9426-a48a63185696}

Name                 MemberType Definition
----                 ---------- ----------
QueryApplicationFile Method     void QueryApplicationFile (string, string, string, bool, bool, SAFEARRAY(Variant))

I'm afraid that is all the info PowerShell will give you in this case.

Sign up to request clarification or add additional context in comments.

3 Comments

I updated my question to add a bit of clarity... I usually don't get it right in the initial post :) In the above example, I'm trying to get the member definitions for the initialized COM object. Where I need to go from there is get the parameter types for each method.
Yes, I think you are completely right about getting the correct definition for the COM objects. Even when trying my example above, I only get the type names and no parameter names... probably has something to do with how COM interfaces work. Thanks for your help and quick responses!
Bummer, I guess I'll just have to construct the definitions myself or try parsing the parameters within the definition... although without the parameter names, it doesn't make much sense. Thanks for the help!
0

I know this is some time back and I fell upon this when trying to get the same answer, thinking it must be possible somehow. Would you know it, it is! Here's a demo:

Class SomeClass {
    $somevar
    SomeClass($somevar){
        # Construct
        $this.somevar = $somevar
    }

    [void]SomeMethod([string]$arg1,[object]$arg2,$arg3){
        # Do a thing
    }
}

Get all method names:

([type]"SomeClass").GetMethods() | Select-Object Name
Name
----
get_somevar
set_somevar
SomeMethod
GetType
ToString
Equals
GetHashCode

Retrieve the args for the named method:

([type]"SomeClass").GetMethod("SomeMethod").GetParameters()

ParameterType    : System.String
Name             : arg1
HasDefaultValue  : False
DefaultValue     : 
RawDefaultValue  : 
MetadataToken    : 134217730
Attributes       : None
Member           : Void SomeMethod(System.String, System.Object, System.Object)
Position         : 0
IsIn             : False
IsLcid           : False
IsOptional       : False
IsOut            : False
IsRetval         : False
CustomAttributes : {}

ParameterType    : System.Object
Name             : arg2
HasDefaultValue  : False
DefaultValue     : 
RawDefaultValue  : 
MetadataToken    : 134217731
Attributes       : None
Member           : Void SomeMethod(System.String, System.Object, System.Object)
Position         : 1
IsIn             : False
IsLcid           : False
IsOptional       : False
IsOut            : False
IsRetval         : False
CustomAttributes : {}

ParameterType    : System.Object
Name             : arg3
HasDefaultValue  : False
DefaultValue     : 
RawDefaultValue  : 
MetadataToken    : 134217732
Attributes       : None
Member           : Void SomeMethod(System.String, System.Object, System.Object)
Position         : 2
IsIn             : False
IsLcid           : False
IsOptional       : False
IsOut            : False
IsRetval         : False
CustomAttributes : {}

I hope this helps if anyone else ends up lurking around here!

Comments

0

Run the method without the parentheses:

$a = 'foo'
$a.replace

OverloadDefinitions
-------------------
string Replace(char oldChar, char newChar)
string Replace(string oldValue, string newValue)

Com objects don't work as well:

$comObj = New-Object -ComObject COMAdmin.COMAdminCatalog
$comobj.AliasComponent

OverloadDefinitions
-------------------
void AliasComponent (string, string, string, string, string)

Comments

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.