Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Parameters for Methods in PowerShell

Tags:

powershell

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.

like image 790
Joshua Dale Avatar asked Dec 12 '12 16:12

Joshua Dale


People also ask

How do I get-command parameters in PowerShell?

You can use the Get-Command cmdlet to provide information about the default parameter set for a cmdlet.

What is param () in PowerShell?

The PowerShell parameter is a fundamental component of any script. A parameter is a way that developers enable script users to provide input at runtime. If a PowerShell script's behavior needs to change in some way, a parameter provides an opportunity to do so without changing the underlying code.

How do I get the properties of an object in PowerShell?

Object properties To get the properties of an object, use the Get-Member cmdlet. For example, to get the properties of a FileInfo object, use the Get-ChildItem cmdlet to get the FileInfo object that represents a file. Then, use a pipeline operator ( | ) to send the FileInfo object to Get-Member .

How do you pass parameters to a function in PowerShell?

You can pass the parameters in the PowerShell function and to catch those parameters, you need to use the arguments. Generally, when you use variables outside the function, you really don't need to pass the argument because the variable is itself a Public and can be accessible inside the function.


1 Answers

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.

like image 129
Keith Hill Avatar answered Oct 07 '22 21:10

Keith Hill