Created
June 24, 2024 08:41
-
-
Save PanosGreg/bbb7f9736beae413addf96d671ef7085 to your computer and use it in GitHub Desktop.
PowerShell Parameter Sets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Parameter Sets Examples | |
# Example #1 | |
# Choose 1 or 2 out of 2 | |
# order is not important | |
<# Combinations: 3 in total | |
1) A | |
2) B | |
3) A & B | |
#> | |
# Example #2 | |
# Choose 1 or 2 or 3 out of 3 | |
# order is not important | |
<# Combinations: 7 in total | |
1) A | |
2) B | |
3) C | |
4) A & B | |
5) A & C | |
6) B & C | |
7) A & B & C | |
#> | |
# Example #3 | |
# Choose 1 or 2 out of 4 | |
# but always need to have the 1st or 2nd, but can't have both 1st and 2nd | |
# order is not important | |
<# Combinations: 6 in total | |
1) A | |
2) B | |
3) A & C | |
4) A & D | |
5) B & C | |
6) B & D | |
#> | |
# Question: What does it mean that: order is not important | |
# Answer: For example B & C is the same as C & B | |
# so this is considered 1 combination out of the total (not two separate combinations) | |
# Now let's do the above with parameter sets in PowerShell | |
# Example #1 | |
function Get-ParamSet1 { | |
<# | |
. SYNOPSIS | |
Combinations: 3 | |
- Version | |
- Role | |
- Version & Role | |
The function will always ask for at least one parameter, | |
either the version or the role. | |
The date is optional, even if you provide a date, | |
the function will still ask for at least one of the other parameters, namely version or role | |
#> | |
[CmdletBinding(DefaultParameterSetName = 'Version')] | |
param ( | |
[Parameter(Mandatory,Position=0,ParameterSetName = 'Version')] | |
[Parameter(Mandatory,Position=0,ParameterSetName = 'VersionAndRole')] | |
[string]$Version, | |
[Parameter(Mandatory,Position=1,ParameterSetName = 'Role')] | |
[Parameter(Mandatory,Position=1,ParameterSetName = 'VersionAndRole')] | |
[string]$Role, | |
[datetime]$Date # <-- optional parameter | |
) | |
# show the parameter set that was used | |
$PSCmdlet.ParameterSetName | |
} | |
# Example #2 | |
function Get-ParamSet2 { | |
<# | |
. SYNOPSIS | |
Combinations: 7 | |
- Version | |
- Role | |
- Group | |
- Version & Role | |
- Version & Group | |
- Role & Group | |
- Version & ROle & Group | |
#> | |
[CmdletBinding(DefaultParameterSetName = 'Version')] | |
param ( | |
[Parameter(Mandatory,Position=0,ParameterSetName = 'Version')] | |
[Parameter(Mandatory,Position=0,ParameterSetName = 'VersionAndRole')] | |
[Parameter(Mandatory,Position=0,ParameterSetName = 'VersionAndGroup')] | |
[Parameter(Mandatory,Position=0,ParameterSetName = 'VersionAndRoleAndGroup')] | |
[string]$Version, | |
[Parameter(Mandatory,Position=1,ParameterSetName = 'Role')] | |
[Parameter(Mandatory,Position=1,ParameterSetName = 'VersionAndRole')] | |
[Parameter(Mandatory,Position=1,ParameterSetName = 'RoleAndGroup')] | |
[Parameter(Mandatory,Position=1,ParameterSetName = 'VersionAndRoleAndGroup')] | |
[string]$Role, | |
[Parameter(Mandatory,Position=3,ParameterSetName = 'Group')] | |
[Parameter(Mandatory,Position=3,ParameterSetName = 'VersionAndGroup')] | |
[Parameter(Mandatory,Position=3,ParameterSetName = 'RoleAndGroup')] | |
[Parameter(Mandatory,Position=3,ParameterSetName = 'VersionAndRoleAndGroup')] | |
[string]$Group, | |
[datetime]$Date | |
) | |
# show the parameter set that was used | |
$PSCmdlet.ParameterSetName | |
} | |
# Example #3 | |
function Get-ParamSet3 { | |
<# | |
. SYNOPSIS | |
Combinations: 6 | |
- Block | |
- File | |
- File & Int | |
- File & Hash | |
- Block & Int | |
- Block & Hash | |
#> | |
[CmdletBinding(DefaultParameterSetName = 'Scriptblock')] | |
param ( | |
[Parameter(Mandatory,Position=0)] | |
[string]$Name, | |
[Parameter(Mandatory,Position=1,ParameterSetName = 'Scriptblock')] | |
[Parameter(Mandatory,Position=1,ParameterSetName = 'BlockAndInt')] | |
[Parameter(Mandatory,Position=1,ParameterSetName = 'BlockAndHash')] | |
[scriptblock]$ScriptBlock, | |
[Parameter(Mandatory,Position=1,ParameterSetName = 'Scriptfile')] | |
[Parameter(Mandatory,Position=1,ParameterSetName = 'FileAndInt')] | |
[Parameter(Mandatory,Position=1,ParameterSetName = 'FileAndHash')] | |
[string]$ScriptFile, | |
[Parameter(Mandatory,Position=2,ParameterSetName = 'BlockAndInt')] | |
[Parameter(Mandatory,Position=2,ParameterSetName = 'FileAndInt')] | |
[int]$Size, | |
[Parameter(Mandatory,Position=2,ParameterSetName = 'BlockAndHash')] | |
[Parameter(Mandatory,Position=2,ParameterSetName = 'FileAndHash')] | |
[hashtable]$Hash, | |
[datetime]$Date | |
) | |
# show the parameter set that was used | |
$PSCmdlet.ParameterSetName | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment