DynamicParameter.psm1

Function New-DynamicParameter
{
    <#
        .SYNOPSIS
        Creates a Dynamic Parameter (RuntimeDefinedParameter) in one command.
 
        .DESCRIPTION
        With one command, it creates a "RuntimeDefinedParameter" for use in a DynamicParam scriptblock.
 
        .PARAMETER Name
        The name of the parameter.
 
        .PARAMETER ValidatedItems
        The set of values for which the dynamic parameter will use.
 
        .PARAMETER Attributes
        A hashtable of parameter attributes to apply to the parameter. (e.g. - "Mandatory", "Position", etc.)
 
        .PARAMETER Aliases
        A list of parameter "aliases" for the dynamic parameter.
 
        .PARAMETER RuntimeType
        The type of the parameter. The default value is [System.String].
 
        .EXAMPLE
        New-DynamicParameter -Name "FileName" -Attributes @{ Mandatory = $true; Position = 0; ParameterSetName = "ByFileName" } -Aliases "name", "file" -ValidatedItems (gci -File *).Name
    #>

    [CmdletBinding(PositionalBinding=$false)]
    [OutputType([System.Management.Automation.RuntimeDefinedParameter])]
    param
    (
        [parameter(Mandatory=$true)]
        [string] $Name
        ,
        [parameter(Mandatory=$true)]
        [array] $ValidatedItems
        ,
        [hashtable] $Attributes
        ,
        [array] $Aliases
        ,
        [type] $RuntimeType = [type]::GetType("System.String")
    )
    $attCol = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
    $valSet = New-Object System.Management.Automation.ValidateSetAttribute($ValidatedItems)
    $attCol.Add($valSet) > $null
    if ($PSBoundParameters["Attributes"])
    {
        $pAtt = New-Object System.Management.Automation.ParameterAttribute -Property $Attributes
        $attCol.Add($pAtt) > $null
    }
    if ($PSBoundParameters["Aliases"])
    {
        $pAlias = New-Object System.Management.Automation.AliasAttribute($Aliases)
        $attCol.Add($pAlias) > $null
    }
    $rtParam = [System.Management.Automation.RuntimeDefinedParameter]::new($Name, $RuntimeType, $attCol)
    return $rtParam
}

Function New-DynamicDictionary
{
    <#
    .SYNOPSIS
    Creates a dictionary of dynamic parameters.
 
    .DESCRIPTION
    Creates a RuntimeDefinedParameterDictionary of 1 or multiple dynamic parameters.
 
    .PARAMETER Parameters
    A list of 1 or more Dynamic Parameters (RuntimeDefinedParameters).
 
    .EXAMPLE
    DynamicParam
    {
        $dynParam1 = New-DynamicParameter -Name "FileName" -Attributes @{ Mandatory = $true; Position = 0; ParameterSetName = "ByFileName" } -Aliases "name", "file" -ValidatedItems (gci -File *).Name
        $dynParam2 = New-DynamicParameter -Name "FolderName" -Attributes @{ Mandatory = $false; Position = 1; ParameterSetName = "ByFolderName" } -Aliases "folder" -ValidatedItems (gci -Directory *).Name
        return $(New-DynamicDictionary -Parameters $dynParam1, $dynParam2)
    }
    #>

    [CmdletBinding(PositionalBinding=$false)]
    [OutputType([System.Management.Automation.RuntimeDefinedParameterDictionary])]
    param
    (
        [parameter(Mandatory=$true,Position=0)]
        [System.Management.Automation.RuntimeDefinedParameter[]] $Parameters
    )
    $rtDict = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
    foreach ($p in $Parameters)
    {
        $rtDict.Add($p.Name, $p) > $null
    }
    return $rtDict
}