Hello!

Someone earlier asked me about ParameterSets, and so I wrote this script to help demonstrate how they work. In short, they help give us options to call a set of parameters. We can also use Mandatory to ensure that all the parameters within that ParameterSets are set. This is optional, but is useful as the manadatory property is only active when you are using the ParameterSets. You can also use PSBoundParameters to dewtermine which PartameterSet has been used and execute one path or another.

ParameterSets are great because they help make functions more flexible as they evolve.


Function Import-TestMandatoryParams {
    param(
        [parameter(Mandatory = $true, ParameterSetName = 'zeta')][string] $z,
        [parameter(Mandatory = $true, ParameterSetName = 'alphabetakappa')][string] $a,
        [parameter(Mandatory = $true, ParameterSetName = 'alphabetakappa')][string] $b,
        [parameter(Mandatory = $true, ParameterSetName = 'alphabetakappa')][string] $k
    )
    if ($PSBoundParameters.ContainsKey('z') -eq $true) {
        Write-Host $z
    }
    else {
        Write-Host $a $b $k
    }
}

# Import-TestMandatoryParams -a "bob" #wont work; will ask for params to be supplied for b and c

# Import-TestMandatoryParams -a "bob" -b "tim" #wont work; will ask for params to be supplied for c

# Import-TestMandatoryParams -a "bob " -b "tim " -k "alf" #will work

# Import-TestMandatoryParams -z "ben" #will work

# Import-TestMandatoryParams -z "ben" -b "tim" #wont work; mixing of paramsets

# Import-TestMandatoryParams -a "bob" -b "tim" -k "alf" -z "ben" #won't work; mixing of paramsets