使用 ValidateSet 为强制 Cmdlet 参数提供默认值
Supply a default value to a mandatory Cmdlet parameter with a ValidateSet
假设一个 Cmdlet class:
[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
的强制参数分配默认值?
你不能。
强制参数必须提供一个值。在提示符下按 Enter 意味着您输入的是空字符串或 null; ValidateSet
与它没有任何关系。即使没有验证,也不会通过在该提示下按 Enter 来分配默认值。
您可以将其设置为可选,然后为其提供默认值,但如果您在调用 cmdlet 时不提供它,它将使用默认值并且不会提示。
假设一个 Cmdlet class:
[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
的强制参数分配默认值?
你不能。
强制参数必须提供一个值。在提示符下按 Enter 意味着您输入的是空字符串或 null; ValidateSet
与它没有任何关系。即使没有验证,也不会通过在该提示下按 Enter 来分配默认值。
您可以将其设置为可选,然后为其提供默认值,但如果您在调用 cmdlet 时不提供它,它将使用默认值并且不会提示。