1

I have the following param() block with a lot of ParameterSetNames

This is inside a function that I use to update, delete or create new rows in a SQL table.

  • Set-User # this will use "List"
  • Set-User user value # this will use "DoWork"
  • Set-User user value -new # this will use "New"
  • Set-User user -delete # this will use "Delete"

but since I added the "Delete" and "New" ParameterSets, I can't use "DoWork" anymore. Why?

Error (unfortunately in German):

Set-User : Der Parametersatz kann mit den angegebenen benannten Parametern nicht aufgelöst werden. In Zeile:1 Zeichen:1 + set-user user value + ~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Set-user], ParameterBindingException + FullyQualifiedErrorId : AmbiguousParameterSet,Set-user

function Set-User {
    [CmdLetBinding(DefaultParameterSetName = "list")]
    param(
        [Parameter(ParameterSetName = "DoWork", Mandatory = $true, Position = 0)]
        [Parameter(ParameterSetName = "New", Mandatory = $true, Position = 0)]
        [Parameter(ParameterSetName = "Delete", Mandatory = $true, Position = 0)]
        [Alias("d")]
        [string]$domain,
        [Parameter(ParameterSetName = "DoWork", Mandatory = $true, Position = 1)]
        [Parameter(ParameterSetName = "New", Mandatory = $true, Position = 1)]
        [Alias("t")]
        [ValidateSet("XMLOrder","EdiOrder","EmailOrder","Edi")]
        [string]$type,
        [Parameter(ParameterSetName = "list")]
        [Alias("l")]
        [switch]$list, 
        [Parameter(ParameterSetName = "New")]
        [switch]$new,
        [Parameter(ParameterSetName = "Delete")]
        [switch]$delete,
        [string]$connectionstring
    )
}

1 Answer 1

2

The error message is quite clear: PowerShell cannot resolve which parameter set to use in the case where you called it and received that error ("AmbiguousParameterSet"). The easiest way to correct this is to make at least one parameter in each parameter set that is unique to that parameter set a mandatory parameter; when I write functions like this, I usually make it the switch parameter. So, in this case, you'd make $new a mandatory parameter, and if you do not specify -new in the call, it will be able to resolve the call's parameter set to DoWork.

[Parameter(Mandatory=$true,ParameterSetName="New")]
[switch]$New

Incidentally, you don't need the [Alias("...")] on two of the parameters you've defined them for; PowerShell will take an unambiguous leading substring as an "alias" for the parameter - so since there's only one parameter that starts with each of 't' and 'l', calling with -t or -l will assume that you mean -type or -list respectively. If you omitted it from -domain, calling with -d would be ambiguous; you might have meant either -domain or -delete - but calling it with -del and -dom would resolve the ambiguity, as does establishing the -d alias.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.