I am having a hard time understanding how Powershell resolves syntax when using dynamic parameters.
A simple, reproducible example:
using System;
using System.Management.Automation;
namespace SOQuestion
{
[Cmdlet(VerbsCommon.Get, "Greeting")]
public class GetGreeting : PSCmdlet, IDynamicParameters
{
[Parameter(Position = 0, Mandatory = true)]
public NameType NameType { get; set; }
private object context;
public object GetDynamicParameters()
{
switch (NameType)
{
case NameType.First:
context = new DynParamsFirst();
return context;
case NameType.Full:
context = new DynParamsFull();
return context;
default:
throw new ArgumentException("Invalid type specified");
}
}
protected override void ProcessRecord()
{
string name;
switch (NameType)
{
case NameType.First:
DynParamsFirst first = context as DynParamsFirst;
name = first.FirstName;
break;
case NameType.Full:
DynParamsFull full = context as DynParamsFull;
name = full.First + " " + full.Last;
break;
default:
throw new ArgumentException("Invalid type specified");
}
WriteObject($"Hello {name}!");
}
}
public enum NameType
{
First = 0,
Full = 1
}
public class DynParamsFirst
{
[Parameter(Position = 1, Mandatory = true)]
public string FirstName { get; set; }
}
public class DynParamsFull
{
[Parameter(Position = 1, Mandatory = true)]
public string First { get; set; }
[Parameter(Position = 2, Mandatory = true)]
public string Last { get; set; }
}
}
This runs as expected when supplying the parameters in the CLI, my parameters are nicely dynamic, and I get the expected output.
But when I run Get-Command Get-Greeting -syntax I get:
Get-Greeting [-NameType] <NameType> [-FirstName] <string> [<CommonParameters>]
Those are the parameters that belong to DynParamsFirst. There is no indication of parameters being dynamic. I found that the syntax being pulled is the syntax for the first match in the NameType enum - if I set Full to 0 in the NameType enum, then Get-Command Get-Greeting -syntax will output the parameters for DynParamsFull instead. Also, most likely related, if I enter Get-Greeting without parameters and hit Enter, I always get just FirstName, which throws an error in combination with the Full NameType.
I find that surprising and it is giving a hard time documenting the command. I also find it non-trivial to formulate a proper question.
Here goes: How do I change the code so that when entering the command without parameters, Powershell is aware that there are dynamic parameters involved?