function Test-Function {
[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='Medium', DefaultParameterSetName='Set')]
param([Parameter(Position=0)]
[string] $Value,
[Parameter(Position=1, ParameterSetName='Set')]
[switch] $SetValue,
[Parameter(Position=1, ParameterSetName='AnotherSet')]
[switch] $AnotherSetValue,
[Parameter(Position=2, ParameterSetName='Set')]
[Parameter(ParameterSetName='AnotherSet')]
[string] $BothSets)
dynamicparam {
if ($PSCmdlet.ParameterSetName -eq 'Set') {
# Build parameter for Set
}
else {
# Build parameter for AnotherSet
}
}
process {
Write-Output "Test"
}
}
$PSCmdlet.ParameterSetName to not reset back to it's default value when the parameter choosing the set has been provided.
Test-Function -Value 'Test':$PSCmdlet.ParameterSetName equals Set (default parameter set, makes sense)Test-Function -Value 'Test' -AnotherSetValue 'SetParam':$PSCmdlet.ParameterSetName equals AnotherSet (also makes sense because now we have a parameter provided that chooses a set that is not the default)Test-Function -Value 'Test' -AnotherSetValue 'SetParam' -BothSets 'BothSetsValue':$PSCmdlet.ParameterSetName equals Set (I do not understand why this makes sense, we have provided AnotherSetValue in the provided parameters, why do I not see the chosen parameter set name being set?)Note: the same behavior occurs even if BothSets does not have ParameterSetName defined.
Further note: I did other testing with trying to read the values of already provided parameters and it seems the dynamparam block only has visibility to the parameter currently in focus or being typed in the console.
Is this expected behavior, if so how can I build a dynamic parameter queuing off parameter data???
> $PSVersionTable
Name Value
---- -----
PSVersion 6.1.0-preview.2
PSEdition Core
GitCommitId v6.1.0-preview.2
OS Microsoft Windows 10.0.16299
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
@LethiferousMoose The parameters returned by the dynamicparameter statement are actually _involved_ in determining the final active parameter set. You can't know which parameter set will ultimately be selected until all phases of parameter binding are done, up to and including binding from the pipeline if any parameters are so attributed. So you certainly can't rely on the parameter set being fixed when the dynamicparameters block is invoked.
Most helpful comment
@LethiferousMoose The parameters returned by the
dynamicparameterstatement are actually _involved_ in determining the final active parameter set. You can't know which parameter set will ultimately be selected until all phases of parameter binding are done, up to and including binding from the pipeline if any parameters are so attributed. So you certainly can't rely on the parameter set being fixed when thedynamicparametersblock is invoked.