0

I'm developing PowerShell Cmdlets in C#. I would like to define mandatory parameters, but that are mutually exclusive.

Example:

Parameter A
Parameter B

When invoking the command, I must set only parameter A or only parameter B, but I cannot set both and I cannot set none.

1
  • 1
    Not sure there is any kind of aspect, or modifier which will allow you to enforce this without just doing parameter validation in your cmdlet code. Why not just do that ? Commented Mar 28, 2024 at 10:17

1 Answer 1

2

You want to use Parameter Sets. You can think of them like C# overloaded functions.

[Cmdlet(...)]
public sealed class MyExample : Cmdlet
{
    [Parameter(Mandatory = true, ParameterSetName = "only parameter A")]
    public string ParameterA { get; set; }

    [Parameter(Mandatory = true, ParameterSetName = "only parameter B")]
    public string ParameterB { get; set; }
}

Just found this for the first time as another resource! https://learn.microsoft.com/en-us/powershell/scripting/developer/cmdlet/cmdlet-parameter-sets?view=powershell-7.4

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.