共用方式為


ValidatePattern 屬性宣告

ValidatePattern 屬性會指定正則表示式模式,以驗證 Cmdlet 參數的自變數。 Windows PowerShell 函式也可以使用此屬性。

在 Cmdlet 內叫用 ValidatePattern 時,Windows PowerShell 運行時間會將 Cmdlet 參數的自變數轉換成字元串,然後將該字串與 ValidatePattern 屬性所提供的模式進行比較。 只有在自變數的轉換字串表示和提供的模式相符時,才會執行 Cmdlet。 如果不符合,Windows PowerShell 運行時間就會擲回錯誤。

語法

[ValidatePattern(string regexString)]
[ValidatePattern(string regexString, Named Parameters)]

參數

RegexStringSystem.String) 必要。 指定驗證參數自變數的正規表達式。

選項 (System.Text.RegularExpressions.RegexOptions) 選擇性具名參數。 指定指定正則表達式選項之 System.Text.RegularExpressions.RegexOptions 旗標的位元組合。

備註

  • 每個參數只能使用這個屬性一次。

  • 您可以使用 屬性的 Option 參數進一步定義模式。 例如,您可以區分模式大小寫。

  • 如果此屬性套用至集合,集合中的每個項目都必須符合模式。

  • ValidatePattern 屬性是由 System.Management.Automation.ValidatePatternAttribute 類別所定義。

另請參閱

System.Management.Automation.ValidatePatternAttribute

撰寫 Windows PowerShell Cmdlet