没有CmdletBinding(),动态参数不起作用

问题描述 投票:3回答:1

该参数似乎根本没有“设置”为参数。验证集不起作用。也没有自动完成功能。输入参数名称也不起作用。

我知道之前我做过动态参数。但这一次,我错过了一些东西。只是无法弄清楚它是什么。

Function Add-Control() {
    DynamicParam {
        $ParamAttribute = New-Object Parameter
        $ParamAttribute.Mandatory = $true
        $ParamAttribute.ParameterSetName  = '__AllParameterSets'

        $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]

        $AttributeCollection.Add($ParamAttribute)

        $controlTypes = @("TextBox", "Label", "DataGrid")

        $AttributeCollection.Add((New-Object ValidateSet($controlTypes)))

        $RuntimeParam = New-Object System.Management.Automation.RuntimeDefinedParameter('Type', [string], $AttributeCollection)

        $RuntimeParamDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary

        $RuntimeParamDictionary.Add('Type', $RuntimeParam)

        return $RuntimeParamDictionary
    }

    Process {
        Write-Host ($PSBoundParameters['Type'])
    }
}

Add-Control -Type "Test"
# $null
powershell
1个回答
3
投票

不确定这是不是一个愚蠢的错误,但我确实有这种感觉。我失踪了

[CmdletBinding()]
Param()

现在验证集和自动完成工作。

希望这有助于其他人。

© www.soinside.com 2019 - 2024. All rights reserved.