使用ValidateSet的静态数组参数与动态参数之间的问题

时间:2019-08-05 13:53:30

标签: arrays powershell parameters

这是我功能的一部分:

function ADManagement-ListMachine() {  
    [CmdletBinding()]

    Param(
            #Many param but only show you the one interesting
        [Alias("Env","Envs")]
        [ValidateSet("PROD","BCP","EACC","IACC")]
            [String[]]$Environments
    )

    DynamicParam {

        #Projects dynamic param
        $Attributes=New-Object System.Management.Automation.ParameterAttribute
        $Attributes.ParameterSetName="Projects"
        $Attributes.Mandatory=$False

        $AttributeCollection=New-Object -Type System.Collections.ObjectModel.Collection[System.Attribute]
        $AttributeCollection.Add($Attributes)

        $Value=Get-ADOrganizationalUnit -Filter * | ?{($_.Name -match "SRV_") -and ($_.Name -notmatch "_prd") -and ($_.Name -notmatch "_noprd")} | %{$_.Name-replace "SRV_",""}
        $ValidateSetAttribute=New-Object System.Management.Automation.ValidateSetAttribute($Value)
        $AttributeCollection.Add($ValidateSetAttribute)

        $DynParam1=New-Object -Type System.Management.Automation.RuntimeDefinedParameter("Projects", [String[]], $attributeCollection)

        $ParamDictionary=New-Object -Type System.Management.Automation.RuntimeDefinedParameterDictionary
        $ParamDictionary.Add("Projects", $DynParam1)
        ###

        return $ParamDictionary
    }

    Begin { 
                #Code
    }

    Process {
       #Code
    }

    End {
       #Code
    }
}

所以,在某些情况下,自动补全功能不起作用:

  

ADManagement-ListMachine -Env PROD,BCP -Projects #tab无效

但是,如果我先使用“ Projects”参数,或者没有为“ Env”参数设置多个值,或者“ Env”参数没有[ValidateSet]定义,它会起作用。

0 个答案:

没有答案