PowerShell dynamic parameter not working with certain parameter types

54 Views Asked by At

When using Dynamic Parmeter in Powershell the autocompletion in the console won't work, if certain (e.g. [datetime] or [pscredential]) datatypes are specified in the Param() section:


    [CmdletBinding()]
    Param(
        [Parameter(
            mandatory=$true,
            Position=1
        )]
        [string]$path,

        [Parameter(
            mandatory=$true,
            Position=2
        )]
        [ValidateSet("Prod","Test")]
        [string]$mode,

        [Parameter(
            mandatory=$true,
            Position=3
        )]
        [datetime]$start
    )

    DynamicParam {
        #Create Paramter Dictionary
        $paramDictionary = new-object System.Management.Automation.RuntimeDefinedParameterDictionary
        
        
        if($mode -eq "Prod")
        {
            #Create Parameter Attribute
            $keyAttribute = New-Object System.Management.Automation.ParameterAttribute
            $keyAttribute.Mandatory = $true
            $keyAttribute.Position = 4
    
            #Create Collection
            $attributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
            $attributeCollection.Add($keyAttribute)
    
            #Create Runtime Defined Parameter
            $keyParam = New-Object System.Management.Automation.RuntimeDefinedParameter('key', [string], $attributeCollection)
    
            #Add the Runtime Defined Parameter to the Paramter Dictionary
            $paramDictionary.Add('key', $keyParam)
        }
    
        return $paramDictionary
    }

    Process {
        <#Stuff will happen here ...#>
    }

If the above script is saved in a file named "dynamic_param.ps1" and called from the console, autocompletion does not insert the "key" parameter when the following line is written and the Tab key is pressed.

.\dynamic_param.ps1 -path "C:\temp\" -mode Prod -start (Get-Date) -

However, if the command is executed without specifying the "Key" parameter, you will be prompted to specify it in the console.

cmdlet dynamic_param.ps1 at command pipeline position 1
Supply values for the following parameters:
key:

If the data type within the paramer is removed or replaced by another one, the autocompletion works again.


    [CmdletBinding()]
    Param(
        [Parameter(
            mandatory=$true,
            Position=1
        )]
        [string]$path,

        [Parameter(
            mandatory=$true,
            Position=2
        )]
        [ValidateSet("Prod","Test")]
        [string]$mode,

        [Parameter(
            mandatory=$true,
            Position=3
        )]
        $start
    )

    DynamicParam {
        #Create Paramter Dictionary
        $paramDictionary = new-object System.Management.Automation.RuntimeDefinedParameterDictionary
        
        
        if($mode -eq "Prod")
        {
            #Create Parameter Attribute
            $keyAttribute = New-Object System.Management.Automation.ParameterAttribute
            $keyAttribute.Mandatory = $true
            $keyAttribute.Position = 4
    
            #Create Collection
            $attributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
            $attributeCollection.Add($keyAttribute)
    
            #Create Runtime Defined Parameter
            $keyParam = New-Object System.Management.Automation.RuntimeDefinedParameter('key', [string], $attributeCollection)
    
            #Add the Runtime Defined Parameter to the Paramter Dictionary
            $paramDictionary.Add('key', $keyParam)
        }
    
        return $paramDictionary
    }

    Process {
        <#Stuff will happen here ...#>
    }

Now the automatic completion in the console works.

.\dynamic_param.ps1 -path "C:\temp\" -mode Prod -start (Get-Date) -key

I tried to not specify the datatype but add a script validation to validate the value inside a script.


[Parameter(
    mandatory=$true,
    Position=3
)]
[ValidateScript({$_ -is [datetime]})]
$start

Sadly, this disables the autocompletion as well.

Things I found

I found this Gitlab issue that describes my problem but was marked as "fixed" back in September 2022.

My question

My question now is if I am doing something wrong or if this is a bug, and if the latter is the case, if anyone has a workaround for this (besides removing the datatype inside the paramter). Thanks in advance for any advice.

0

There are 0 best solutions below