Zholen
Zholen

Reputation: 1790

Getting Get-Help Parameters as C# objects

I am calling Get-Help ... -Full on various scripts to determine what Parameters are needed to run the script. A dynamic form is then displayed for the user to fill out.

I've noticed that Get-Help does not always return the same structure, i.e some scripts return

NAME
    ....

SYNOPSIS

SYNTAX
    ....

DESCRIPTION
    ...

PARAMETERS

while others simply return

test2.ps1 [[-foo] <String>] [[-bar] <String>]

I started down a path to retrieve this information from the PSObject:

PSObject p = (PSObject)results[0].Properties["Parameters"].Value;

foreach (var info in p.Properties)
{
    var b = (PSObject[])info.Value;
    foreach ( var c in b)
    {
        Console.WriteLine(c.Properties["name"].Value);
    }
}

But this fails with the second type of result.

Is there not a more common way to retrieve this information that I have overlooked?

Upvotes: 1

Views: 203

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 175065

I think you might be looking for the output from Get-Command -Syntax

test2 syntax

You can retrieve this information in C# by creating a CommandInfo object and accessing the Parameters and ParameterSets properties:

CommandInfo getHelpCommand = new CmdletInfo("Get-Help", typeof(Microsoft.PowerShell.Commands.GetHelpCommand));
var Params = getHelpCommand.Parameters;

foreach (string paramKey in Params.Keys)
{
    ParameterMetadata currentParam = Params[paramKey];
    Console.Write(currentParam.Name);
}

Upvotes: 3

Related Questions