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?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Zholen
  • 1,762
  • 2
  • 24
  • 53
  • The difference in output is because the scripts showing extended information have [comment-based help](https://technet.microsoft.com/en-us/magazine/ff458353.aspx) while the others just have [`[CmdletBindings()]`](http://stackoverflow.com/q/14671051/1630171) enabled. – Ansgar Wiechers Dec 11 '15 at 23:50

1 Answers1

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);
}
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • +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. – Zholen Dec 14 '15 at 17:18
  • 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] ]`-style string – Mathias R. Jessen Dec 14 '15 at 17:42