假设有一个 Cmdlet 类:
[System.Management.Automation.Cmdlet(System.Management.Automation.VerbsCommon.Get, "LogonToken")]
public class GetLogonToken : System.Management.Automation.Cmdlet
{
// constructor
public GetLogonToken()
{
// set default auth.
this.Authentication = "secWinAD";
}
// addtional properties omitted
[System.Management.Automation.Parameter(Position = 1, Mandatory = true)]
[ValidateSet("secEnterprise", "secLDAP", "secWinAD")]
public string Authentication
{
get { return authentication; }
set { authentication = value; }
}
// setting authentication here has no effect either
private string authentication;
// addtional code omitted
}
调用Cmdlet:
PS ..\bin\Debug> get-logontoken
cmdlet Get-LogonToken at command pipeline position 1
Supply values for the following parameters:
ServerName: SERVER
Authentication: <hit enter>
Username: USERNAME
Password: ********
Get-LogonToken : Cannot validate argument on parameter 'Authentication'. The
argument "" does not belong to the set "secEnterprise,secLDAP,secWinAD"
specified by the ValidateSet attribute. Supply an argument that is in the set
and then try the command again.At line:1 char:1
+ get-logontoken
+ ~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-LogonToken], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,PsEnterprise.GetLogonToken
如何为使用ValidateSet
的必选参数分配默认值?
manditory
。 - wOxxOm