2016-08-31 3 views
2

와 필수 cmdlet은 매개 변수에 기본값 : cmdlet을 호출공급 cmdlet에 클래스 가정 할 ValidateSet

[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 

} 

을 : 나는 필수 매개 변수에 기본값을 할당하려면 어떻게

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을 사용합니까?

+0

제목에 오타를 수정하십시오 :'manditory' – wOxxOm

답변

2

수 없습니다.

필수 매개 변수에는 제공된 값이 있어야합니다. 프롬프트에서 Enter 키를 누르는 것은 빈 문자열이나 널을 의미합니다. ValidateSet과 관련이 없습니다. 유효성 검사가 없더라도 해당 프롬프트에서 Enter 키를 누르면 기본값이 지정되지 않습니다.

선택적으로 설정 한 다음 기본값을 제공 할 수 있지만 cmdlet을 호출 할 때 제공하지 않으면 기본값을 사용하므로 메시지가 표시되지 않습니다.

+0

나는 선택적 매개 변수를 만드는 것으로 생각하고, 생성자에서 기본값을 제공하는 것이 좋은 옵션이라고 생각합니다. 탭 완성이'authentication' 매개 변수의 유효 값을 나열하는 것으로 보이지 않습니다. – craig

+0

@ ValidateSet을 사용하는 @craig는 탭 완성을 허용해야합니다. 그게 왜 효과가 없을지 모르겠습니다. – briantist

+0

탭 완료는 셸 또는 ISE의 ​​32 비트 또는 64 비트 버전에서 작동하지 않았습니다. – craig

관련 문제