0

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?

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
Alex_P
  • 2,580
  • 3
  • 22
  • 37

1 Answers1

2

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() =
    ...
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • that's damn fast lol on fire huh – Santiago Squarzon Dec 14 '22 at 15:59
  • @SantiagoSquarzon [pew pew](https://media.tenor.com/kqkWO-L3xHgAAAAd/lucky-luke-shadow.gif) – Mathias R. Jessen Dec 14 '22 at 16:00
  • Hi Mathias, But then I would have a problem with the `Secret` parameter. Can I interpret your answer that it is not possible to have two mutually exclusive parameters with the same positional value? – Alex_P Dec 14 '22 at 16:01
  • @Alex_P Correct, you can't have 2 defaults, and there's no "magic" available to make PowerShell telepathic at runtime :) It's either 1 or the other – Mathias R. Jessen Dec 14 '22 at 16:03
  • @Alex_P Think about it for a moment. How in the world should PowerShell "know" that the `relativePath` in `Verb-Noun $payload $algorithm relativePath` is a path and not a literal secret value? – Mathias R. Jessen Dec 14 '22 at 16:04
  • @MathiasR.Jessen, well I was hoping the parameter sets would allow this bit. Anyhow, thanks for your answer. – Alex_P Dec 14 '22 at 16:07
  • @Alex_P How would that work? What would happen in the example above? FilePath picked because... why? – Mathias R. Jessen Dec 14 '22 at 16:12