1

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?

1

1 Answer 1

3

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

2 Comments

+1 for directing me to the Get-Command -Syntax, this at least returns me predictable results. I'm confused on the CmdletInfo however. I can't seem to figure out how to actually use it.
The items in the Parameters and ParameterSets dictionaries contain information about whether each parameter is mandatory, what type it expects etc. My point being that you could use this information to determine what the syntax is, and generate your own Command-Name [[-Parameter] <type>]-style string

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.