共用方式為


如何驗證自變數計數

此範例示範如何指定 Windows PowerShell 執行時間可用來檢查參數在執行 Cmdlet 之前所接受的自變數數目(計數) 驗證規則。 您可以宣告 ValidateCount 屬性來設定此驗證規則。

備註

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

驗證自變數計數

  • 新增 Validate 屬性,如下列程式代碼所示。 這個範例會指定 參數將接受一個自變數或多達三個自變數。

    [ValidateCount(1, 3)]
    [Parameter(Position = 0, Mandatory = true)]
    public string[] UserNames
    {
      get { return userNames; }
      set { userNames = value; }
    }
    
    private string[] userNames;
    

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

另請參閱

ValidateCount 屬性宣告

撰寫 Windows PowerShell Cmdlet