powershelldynamicparametersparameter-sets

PowerShell - Can I make a parameter set depend on the value of another parameter?


Let's say I have a function like:

function Authenticate
{
    param
    (
        [ValidateSet('WindowsAuthentication','UsernameAndPassword')]
        [string]$AuthenticationType,

        [Parameter(ParameterSetName='ParamSet1')]
        [string]$Username,

        [Parameter(ParameterSetName='ParamSet1')]
        [string]$Password
    )
  ..
}

And I would like to enforce these rules:

It this possible?


Solution

  • Using the link from Tim Ferrill's answer, I created the following function to help create dynamic parameters:

    function New-DynamicParameter
    {
        [CmdletBinding(DefaultParameterSetName = 'Core')]    
        param
        (
            [Parameter(Mandatory = $true, ValueFromPipeline = $true)][string] $Name,
            [Parameter(Mandatory = $true, ParameterSetName = 'Core')][Parameter(Mandatory = $true, ParameterSetName = 'ValidateSet')][type] $Type,
            [Parameter(Mandatory = $false)][string] $ParameterSetName = '__AllParameterSets',
            [Parameter(Mandatory = $false)][bool] $Mandatory = $false,
            [Parameter(Mandatory = $false)][int] $Position,
            [Parameter(Mandatory = $false)][bool] $ValueFromPipelineByPropertyName = $false,
            [Parameter(Mandatory = $false)][string] $HelpMessage,
            [Parameter(Mandatory = $true, ParameterSetName = 'ValidateSet')][string[]] $ValidateSet,
            [Parameter(Mandatory = $false, ParameterSetName = 'ValidateSet')][bool] $IgnoreCase = $true
        )
    
        process
        {
            # Define Parameter Attributes
            $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute
            $ParameterAttribute.ParameterSetName = $ParameterSetName
            $ParameterAttribute.Mandatory = $Mandatory
            $ParameterAttribute.Position = $Position
            $ParameterAttribute.ValueFromPipelineByPropertyName = $ValueFromPipelineByPropertyName
            $ParameterAttribute.HelpMessage = $HelpMessage
    
            # Define Parameter Validation Options if ValidateSet set was used
            if ($PSCmdlet.ParameterSetName -eq 'ValidateSet')
            {
                $ParameterValidateSet = New-Object System.Management.Automation.ValidateSetAttribute -ArgumentList $ValidateSet -Strict (!$IgnoreCase)
            }
    
            # Add Parameter Attributes and ValidateSet to an Attribute Collection
            $AttributeCollection = New-Object Collections.ObjectModel.Collection[System.Attribute]
            $AttributeCollection.Add($ParameterAttribute)
            $AttributeCollection.Add($ParameterValidateSet)
    
            # Add parameter to parameter list
            $Parameter = New-Object System.Management.Automation.RuntimeDefinedParameter -ArgumentList @($Name, $Type, $AttributeCollection)
    
            # Expose parameter to the namespace
            $ParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
            $ParameterDictionary.Add($Name, $Parameter)
            return $ParameterDictionary
        }
    }
    

    And solved my particular problem in the following way:

    function Authenticate
    {
      param
      (
        [ValidateSet('WindowsAuthentication','UsernameAndPassword')][string] $AuthenticationType,
      )
    
      DynamicParam
      {
        if ($AuthenticationType -eq 'UsernameAndPassword')
        {
          New-DynamicParameter Username [string] -Mandatory $true
          New-DynamicParameter Password [string] -Mandatory $true
        }
      }
    
      ...
    }
    

    It became unneeded to have a parameter set when using Dynamic Parameter so I removed the parameter set.