ValidateSet() 修饰不会生成制表符补全

ValidateSet() decoration doesn't generate tab completion

Shay Levy 在 POWERSHELL SCRIPTING WITH [VALIDATESET] 中的评论建议用 ValidateSet() 修饰 PowerShell 参数应该导致制表符完成:

One of the benfits of using ValidateSet (or Enum types) in PowerShell 3.0 is that you get tab expansion on the ValidateSet/Enum values.

我似乎无法在 PowerShell 函数或 C# Cmdlet 中使用它。

函数:

function Get-LogonToken {

    [CmdletBinding()]
    param(

        [Parameter(Position = 0, Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [string] $ServerName,

        [Parameter(Position = 1, Mandatory = $true)]
        [ValidateSet("secEnterprise", "secLDAP", "secWinAD")]
        [string] $Authentication,

        [Parameter(Position = 2, Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [string] $Username,

        [Parameter(Position = 3, Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [SecureString] $Password

    )

    Write-Verbose "ServerName: $ServerName"
    Write-Verbose "Authentication: $Authentication"
    Write-Verbose "Username: $Username"
    Write-Verbose "Password: $(ConvertFrom-SecureString $Password)"

    ...
}

Cmdlet:

[System.Management.Automation.Cmdlet(System.Management.Automation.VerbsCommon.Get, "LogonToken")]
    [OutputType(typeof(System.String))]
    public class GetLogonToken : System.Management.Automation.Cmdlet
    {

        [System.Management.Automation.Parameter(Position = 0, Mandatory = true)]
        [ValidateNotNullOrEmpty]
        public string ServerName
        {
            get { return server; }
            set { server = value; }
        }
        private string server;

        [System.Management.Automation.Parameter(Position = 1, Mandatory = true)]
        [ValidateSet("secEnterprise", "secLDAP", "secWinAD")]
        public string Authentication
        {
            get { return authentication; }
            set { authentication = value; }
        }
        private string authentication;

        [System.Management.Automation.Parameter(Position = 2, Mandatory = true)]
        [ValidateNotNullOrEmpty]
        public string Username
        {
            get { return username; }
            set { username = value; }
        }
        private string username;

        [System.Management.Automation.Parameter(Position = 3, Mandatory = true)]
        [ValidateNotNullOrEmpty]
        public SecureString Password
        {
            get { return password; }
            set { password = value; }
        }
        private SecureString password;

  ...

} 

PowerShell:

PS> get-logontoken -verbose

cmdlet Get-LogonToken at command pipeline position 1
Supply values for the following parameters:
ServerName: server
Authentication: sec<tab> (nothing generated)

我错过了什么?

感谢克雷格作为一个单独的问题发帖。现在我明白问题是什么了。

当 PowerShell 提示您缺少 Mandatory 值时(在 运行 时),您无法使用 Tab 键完成,仅当您第一次输入 cmdlet 时:

Get-LogonToken -Authentication secTAB