动态参数输入后未显示静态验证集参数

时间:2017-09-04 23:14:29

标签: powershell

我使用动态和静态参数的混合发现了以下情况,我想知道我做错了什么,或者是否存在面对这种情况的自动完成的错误/意外行为。

我有两个功能。

Test-MyExampleSet -Service 'Service 1' # Auto-Completion does not suggest -Name parameter

Test-MyExample -Service 'Service 1' -Name AdminTools # Auto-completion works fine.

第一个使用validateset和动态参数的静态参数。

第二个具有静态参数(字符串)和dynamyc参数。 使用Test-MyExampleSet时,在添加-Service参数后,自动完成不会在任何位置显示-Name动态参数。

使用Test-MyExample重复完全相同的事情,这与减去验证集完全相同,可以正常工作。

是否与我声明动态参数的方式有关,还是混合动态参数和静态参数确实存在问题?

包含2个函数定义的示例代码

$include = @("Service 1", "Service 2")

function Test-MyExample
{
[cmdletBinding()]
  param
  (
    [Parameter(Position=-0,Mandatory=$false)]
     [String]$Service
  )
   DynamicParam
    {
        # Set the dynamic parameters name
        $ParameterName = 'Name'

        # Create the dictionary
        $RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary

        # Create the collection of attributes
        $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]

        # Create and set the parameters' attributes
        $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute
        $ParameterAttribute.ValueFromPipeline = $true
        $ParameterAttribute.ValueFromPipelineByPropertyName = $true
        $ParameterAttribute.Mandatory = $true
        $ParameterAttribute.Position = 0
        $ParameterAttribute.ParameterSetName = "__AllParameterSets"
        # Add the attributes to the attributes collection
        $AttributeCollection.Add($ParameterAttribute)

        # Generate and set the ValidateSet
        $arrSet = [Enum]::GetNames('System.Environment+SpecialFolder')
        $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet)

        # Add the ValidateSet to the attributes collection
        $AttributeCollection.Add($ValidateSetAttribute)

        # Create and return the dynamic parameter
        $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName, [string], $AttributeCollection)
        $RuntimeParameterDictionary.Add($ParameterName, $RuntimeParameter)
        return $RuntimeParameterDictionary
    }

    Begin
    {
        # Bind the parameter to a friendly variable
        $Name = $PsBoundParameters[$ParameterName]
    }

    Process
    {
        $Name | ForEach-Object { [Environment]::GetFolderPath($_) }
    }
    }

    function Test-MyExampleSet
{
[cmdletBinding()]
  param
  (
  [ValidateSet('Service 1','Service 2')]
  [Parameter(Position=-0,Mandatory=$false)]
     [String]$Service
  )
   DynamicParam
    {
        # Set the dynamic parameters name
        $ParameterName = 'Name'

        # Create the dictionary
        $RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary

        # Create the collection of attributes
        $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]

        # Create and set the parameters' attributes
        $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute
        $ParameterAttribute.ValueFromPipeline = $true
        $ParameterAttribute.ValueFromPipelineByPropertyName = $true
        $ParameterAttribute.Mandatory = $true
        $ParameterAttribute.Position = 0
        $ParameterAttribute.ParameterSetName = "__AllParameterSets"
        # Add the attributes to the attributes collection
        $AttributeCollection.Add($ParameterAttribute)

        # Generate and set the ValidateSet
        $arrSet = [Enum]::GetNames('System.Environment+SpecialFolder')
        $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet)

        # Add the ValidateSet to the attributes collection
        $AttributeCollection.Add($ValidateSetAttribute)

        # Create and return the dynamic parameter
        $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName, [string], $AttributeCollection)
        $RuntimeParameterDictionary.Add($ParameterName, $RuntimeParameter)
        return $RuntimeParameterDictionary
    }

    Begin
    {
        # Bind the parameter to a friendly variable
        $Name = $PsBoundParameters[$ParameterName]
    }

    Process
    {
        $Name | ForEach-Object { [Environment]::GetFolderPath($_) }
    }
    }




Test-MyExampleSet -Service 'Service 1' # Auto-Completion does not suggest -Name parameter

Test-MyExample -Service 'Service 1' -Name AdminTools # Auto-completion works fine.

0 个答案:

没有答案