Hi,
I’m making a function and I’m having trouble with some parametersets…
This is my function.
function Test-ParameterSet { [CmdletBinding( DefaultParameterSetName = "all" )] Param ( [Parameter(Mandatory = $false, Position = 0)] [ValidateNotNullorEmpty()] [String]$apiKey, [ValidateSet("Prod", "Dev")] [Parameter(Mandatory = $false)] $environment = "Dev", [Parameter(Mandatory = $false)] [int]$maxItems, [Parameter(Mandatory = $false, ParameterSetName = "all")] [switch]$all, [Parameter(Mandatory = $false, ParameterSetName = "byCustomerId")] [Parameter(ParameterSetName = "byId")] [Parameter(ParameterSetName = "byName")] [string]$customerId, [Parameter(Mandatory = $false, ParameterSetName = "byId")] [Parameter(ParameterSetName = "byCustomerId")] [string]$id, [Parameter(Mandatory = $false, ParameterSetName = "byName")] [Parameter(ParameterSetName = "byCustomerId")] [string]$name ) Write-Output $PSCmdlet.ParameterSetName }
Basically how I want it to work is;
Test-ParameterSet -All (Gets everything, CustomerId, Name and Id is not available) Test-ParameterSet -Customerid XXX (Gets everything by this particular customer, customerId and Name is available but not mandatory) Test-ParameterSet -CustomerId XXX -id XXX (Gets object from this particular customer with id specified. Name is not available) Test-ParameterSet -CustomerId XXX -name XXX (Gets object from this particular customer with name specified. id is not available)
According to Get-Help this is how it looks
NAME Test-ParameterSet SYNTAX Test-ParameterSet [[-apiKey] ] [-environment {Prod | Dev}] [-maxItems ] [-all] [] Test-ParameterSet [[-apiKey] ] [-environment {Prod | Dev}] [-maxItems ] [-customerId ] [-entityName ] [] Test-ParameterSet [[-apiKey] ] [-environment {Prod | Dev}] [-maxItems ] [-customerId ] [-entityId ] [] Test-ParameterSet [[-apiKey] ] [-environment {Prod | Dev}] [-maxItems ] [-customerId ] [-entityId ] [-entityName ] [] ALIASES None REMARKS None
I am unable to find a way to exclude id if name is used and exclude name if id is used. According to help I should be able to use :
PS C:\Windows\system32> Test-ParameterSet -customerId 1212
But I am recieving an error:
Test-ParameterSet : Parameter set cannot be resolved using the specified named parameters.
At line:1 char:1
-
Test-ParameterSet -customerId 1212
-
-
CategoryInfo : InvalidArgument: ( [Test-ParameterSet], ParameterBindingException
-
FullyQualifiedErrorId : AmbiguousParameterSet,Test-ParameterSet
What am I doing wrong?