Search code examples
c#powershellpowershell-4.0powershell-cmdlet

ValidateSet() decoration doesn't generate tab completion


Shay Levy's comment in POWERSHELL SCRIPTING WITH [VALIDATESET] suggest that decorating a PowerShell parameter with ValidateSet() should result in tab completions:

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.

I can't seem to get this to work in a PowerShell function or C# Cmdlet.

Function:

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)

What am I missing?


Solution

  • Thanks for posting as a separate question craig. Now I see what the problem is.

    You can't tab complete when PowerShell prompts you for a missing Mandatory value (at run-time), only when you're first typing out the cmdlet:

    Get-LogonToken -Authentication secTAB