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

时间:2016-09-01 17:53:57

标签: c# powershell powershell-v4.0 cmdlets cmdlet

Shay Levy在POWERSHELL SCRIPTING WITH [VALIDATESET]中的评论建议使用ValidateSet()修饰PowerShell参数会导致标签符号完成:

  

在PowerShell中使用ValidateSet(或Enum类型)的一个好处   3.0是你在ValidateSet / Enum值上获得标签扩展。

我似乎无法在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)

我错过了什么?

1 个答案:

答案 0 :(得分:1)

感谢您发布单独的问题。现在我明白了问题所在。

当PowerShell提示您缺少Mandatory值(在运行时)时,只有在您首次输入cmdlet时,才能完成标签:

Get-LogonToken -Authentication sec TAB