共用方式為


如何驗證自變數集

此範例示範如何在執行 Cmdlet 之前,指定 Windows PowerShell 運行時間可用來檢查參數自變數的驗證規則。 此驗證規則會提供一組參數自變數的有效值。

備註

如需定義此屬性之類別的詳細資訊,請參閱 System.Management.Automation.ValidateSetAttribute

驗證自變數集

  • 新增 ValidateSet 屬性,如下列程式代碼所示。 本範例會指定一組三個可能值給 UserName 參數。

    [ValidateSet("Steve", "Mary", "Carl", IgnoreCase = true)]
    [Parameter(Position = 0, Mandatory = true)]
    public string UserName
    {
      get { return userName; }
      set { userName = value; }
    }
    
    private string userName;
    

如需如何宣告此屬性的詳細資訊,請參閱 ValidateSet 屬性宣告

另請參閱

System.Management.Automation.ValidateSetAttribute

ValidateSet 屬性宣告

撰寫 Windows PowerShell Cmdlet