How to set positional parameters in parameter sets?

273 views Asked by At

I wrote a function with four parameters. The first two parameters are mandatory for both sets : Payload and Algorithm. The last two parameters (Secret and FilePath) define the parameter set. The order of defined paramters is:

  1. Payload
  2. Algorithm
  3. Secret
  4. FilePath

I set Payload the positional value of 0 and Algorithm the value of 1. As Secret and FilePath are mutually exclusive I set their value to 2.

[<Parameter(
  Position=0,
  Mandatory=true,
  ValueFromPipelineByPropertyName=true)>]
[<ValidateNotNullOrEmpty>]
member val Payload : Hashtable = Hashtable () with get, set
[<Parameter(
  Position=1,
  Mandatory=true,
  ValueFromPipelineByPropertyName=true)>]
member val Algorithm : cryptographyType = { Algorithm = HMAC; Encryption = SHA256 } with get, set
[<Parameter(
  Position=2,
  ParameterSetName="Key",
  Mandatory=true,
  ValueFromPipelineByPropertyName=true)>]
[<ValidateNotNullOrEmpty>]
member val Secret : string = String.Empty with get, set
[<Parameter(
  Position=2,
  ParameterSetName="FilePath",
  Mandatory=true,
  ValueFromPipelineByPropertyName=true)>]
[<ValidateNotNullOrEmpty>]
member val FilePath : System.IO.FileInfo = null with get, set

When I run the function using only the positions, I get a file error when I intend to use the FilePath parameter because the function probably expects it to be the Secret parameter.

Is there a way to use positional parameter for parameter sets?

1

There are 1 answers

7
Mathias R. Jessen On

When I run the function using only the positions, I get a file error when I intend to use the FilePath parameter because the function probably expects it to be the Secret parameter.

If you want the FilePath parameter set to be the default, set the DefaultParameterSetName property on a Cmdlet attribute decorator on the containing type:

[<Cmdlet("Verb", "Noun", DefaultParameterSetName = "FilePath")>]
type MyCmdlet() =
    ...