I am trying to create a function in PowerShell where there is a particular Parameter Switch that then gives other choices, but these are only available for use if this parameter is used in conjunction with the main one. This will be for adding users to AD groups. I am still learning how ParameterSets work.
What I want is for something like this to be possible:
OK: (where 21 and 22 are the multiple choice switches
Set-UserAccess -user Username -Access1 -Access2 -Access21
Set-UserAccess -user Username -Access1 -Access2 -Access21 -Access22
Set-UserAccess -user Username -Access1 -Access2 -Access22
Set-UserAccess -user Username -Access2 -Access22
Not OK:
Set-UserAccess -user Username -Access1 -Access21
Set-UserAccess -user Username -Access1 -Access2
Set-UserAccess -Access21
Can you please advise how to create the Parameter Sets to allow this.
Basically what I have so far is (renamed to be generic for this question)
[CmdletBinding(DefaultParameterSetName='Default')]
Param
(
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)][string[]]
$user,
[Parameter()][switch]$Access1,
[Parameter()][switch]$Access2,
[Parameter()][switch]$Access3,
[Parameter(ParameterSetName='Access4')][Switch]$Access4,
[Parameter(ParameterSetName='Access4')][Switch]$Access4Choice1,
[Parameter(ParameterSetName='Access4')][Switch]$Access4Choice2,
)
Please advise if any of this needs clarifying.
You could define a parameter set for each multiple choice switch (Access4, 5 and 6). However, I would use a string as Parameter with a ValidateSet:
[CmdletBinding(DefaultParameterSetName='Default')]
Param
(
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)][string[]]
$user,
[switch]$Access1,
[switch]$Access2,
[switch]$Access3,
[ValidateSet("Access4Choice1", "Access4Choice2", "Access4Choice3")]
[string]$Access4
)
This will give your a choice of the parameter:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With