0

I want to add two switch parameters to my custom PowerShell cmdlet like this:

[Parameter(Mandatory = false, ParameterSetName = "p1")]
public SwitchParameter switchOne { get; set; }

[Parameter(Mandatory = false, ParameterSetName = "p2")]
public SwitchParameter switchTwo { get; set; }

But when I run my cmdlet, it raises AmbiguousParameterSet, even if I don't specify any of them. Am I missing something here?

7
  • Do you have default parameter set specified in Cmdlet attribute? Commented Sep 11, 2018 at 18:17
  • No, I don't. I guess without the parameters the ambiguity would remain right? Commented Sep 11, 2018 at 18:20
  • 1
    Correct. Without parameters passed, it's entirely ambiguous. [Cmdlet(DefaultParameterSet = "p1")] Commented Sep 11, 2018 at 18:25
  • 1
    @TheIncorrigible1 Note in C# it is Cmdlet attribute, but not CmdletBinding attribute. Commented Sep 11, 2018 at 18:28
  • @PetSerAl Thanks- edited. I obviously don't write cmdlets in C#! Commented Sep 11, 2018 at 18:30

1 Answer 1

1

Set the mandatory attribute to true if the switch uniquely indicates the use of a parameter set.

If this is the case then you may need to reconsider your use of parameters sets, each set should be unique, if you need a switch to indicate uniqueness then you probably don't need parameter sets.

However, without a full list of the parameters its difficult to say.

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

1 Comment

I removed both parameter set names and it worked. I guess the two parameter sets, although with different names, specified the same number and type of parameters (One mandatory, three strings and this two of type switchParameter).

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.