I decided to take a stab at trying to do a parameterset.
param
(
[Parameter(Mandatory = $true,
Position = 0)]
[ValidateSet('Production', 'DR')]
[string]$env,
[Parameter(Mandatory = $true,
Position = 1)]
[ValidateLength(8, 8)]
[string]$date,
[Parameter(Position = 2)]
[pscredential]$credential,
[Parameter(ParameterSetName = 'PPgroup')]
[switch]$PP,
[switch]$incoming,
[Parameter(ParameterSetName = 'Agroup')]
[switch]$A
)
What i am expecting is that when i use PP that I can only see incoming . However when I use the parameter a that it does not show incoming. However when I use the parameter a the option for PP goes away but incoming still remains. What gives?
Any help appreciated.
I would define $date as [Datetime] rather than a [string], and I would separate $pp from $incoming as in:
function TestParameterSets {
param
(
[Parameter(Mandatory = $true, Position = 0)]
[ValidateSet('Production', 'DR')]
[string]$env,
[Parameter(Mandatory = $true, Position = 1)]
[dateTime]$date,
[Parameter(Position = 2)]
[pscredential]$credential,
[Parameter(ParameterSetName = 'PPgroup')]
[switch]$PP,
[Parameter(ParameterSetName = 'PPgroup')]
[switch]$incoming,
[Parameter(ParameterSetName = 'Agroup')]
[switch]$A
)
"env = $env"
"date = $date"
"credential = $(if ($credential) {""(that of '$($credential.UserName)')""} else {$credential}) "
"pp = $pp"
"incoming = $incoming"
"A = $A"
}
Results match expectations:
TestParameterSets -env DR -date (Get-Date) -credential (Get-Credential samb) -incoming
env = DR
date = 02/27/2020 07:17:13
credential = (that of 'samb')
pp = False
incoming = True
A = False
TestParameterSets -env Production -date (Get-Date) -credential (Get-Credential samb) -A
env = Production
date = 02/27/2020 07:13:46
credential = (that of 'samb')
pp = False
incoming = False
A = True
$Incoming shows as $false when using ‘Agroup’ parameterset because it’s declared as a switch which defaults to $false.
I would also make at least one of the unique parameters of each parameter set mandatory. For example $PP and $A. To avoid the error: ‘Parameter set cannot be resolved using the specified named parameters.’