PowerShell for Windows Admins

Jul 20 2015   12:55PM GMT

Input validation on multiple regex

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Tags:
Powershell
Regular expressions

One of the things I like about writing advanced functions is the ability to validate the input. if you test the input immediately you can  often stop mistakes being made. Mistakes that could damage your system!

One of the validation options is to test the input against a regular expression. Not being a big fan of regular expressions I don’t use this often but one option that came up on the forum was testing against more than one regex.

DISCLAIMER – The regex I’m using aer for illustrative purposes only and I don’t claim they are the best way of doing this.

This function uses a simple regex to validate the input starts with a letter:

function Test-Pval
{
[CmdletBinding()]
Param
(
[ValidatePattern(“^[a-z]”)]
[String]
$parm1
)
Write-Host “Input was $parm1”
}

£> Test-Pval abcd
Input was abcd

£> Test-Pval Abcd
Input was Abcd

£> Test-Pval 2bcd
Test-Pval : Cannot validate argument on parameter ‘parm1’. The argument “2bcd” does not match the “^[a-z]” pattern.
Supply an argument that matches “^[a-z]” and try the command again.
At line:1 char:11
+ Test-Pval 2bcd
+           ~~~~
+ CategoryInfo          : InvalidData: (:) [Test-Pval], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Test-Pval

Lets also assume that we want to be able to accept input starting with @ or #

function Test-Pval
{
[CmdletBinding()]
Param
(
#[ValidatePattern(“^[a-z]”)]
[ValidatePattern(“^[@,#]”)]
[String]
$parm1
)
Write-Host “Input was $parm1”
}

£> Test-Pval 2bcd
Test-Pval : Cannot validate argument on parameter ‘parm1’. The argument “2bcd” does not match the “^[@,#]” pattern.
Supply an argument that matches “^[@,#]” and try the command again.
At line:1 char:11
+ Test-Pval 2bcd
+           ~~~~
+ CategoryInfo          : InvalidData: (:) [Test-Pval], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Test-Pval
£> Test-Pval abcd
Test-Pval : Cannot validate argument on parameter ‘parm1’. The argument “abcd” does not match the “^[@,#]” pattern.
Supply an argument that matches “^[@,#]” and try the command again.
At line:1 char:11
+ Test-Pval abcd
+           ~~~~
+ CategoryInfo          : InvalidData: (:) [Test-Pval], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Test-Pval
£> Test-Pval @bcd
Test-Pval : Cannot validate argument on parameter ‘parm1’. The argument “” does not match the “^[@,#]” pattern.
Supply an argument that matches “^[@,#]” and try the command again.
At line:1 char:11
+ Test-Pval @bcd
+           ~~~~
+ CategoryInfo          : InvalidData: (:) [Test-Pval], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Test-Pval
£> Test-Pval ‘@bcd’
Input was @bcd

£> Test-Pval ‘#bcd’
Input was #bcd

£> Test-Pval ‘?bcd’
Test-Pval : Cannot validate argument on parameter ‘parm1’. The argument “?bcd” does not match the “^[@,#]” pattern.
Supply an argument that matches “^[@,#]” and try the command again.
At line:1 char:11
+ Test-Pval ‘?bcd’
+           ~~~~~~
+ CategoryInfo          : InvalidData: (:) [Test-Pval], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Test-Pval

The previous acceptable input is now rejected and only input starting with @ or # is accepted.

To combine the 2 patterns we use the | symbol which is an OR operator in regex

function Test-Pval
{
[CmdletBinding()]
Param
(
[ValidatePattern(“^[a-z | @,#]”)]
[String]
$parm1
)
Write-Host “Input was $parm1”
}

function Test-Pval
{
[CmdletBinding()]
Param
(
[ValidatePattern(“^[a-z | @,#]”)]
[String]
$parm1
)
Write-Host “Input was $parm1”
}

Which when used give this

£> Test-Pval abcd
Input was abcd

£> Test-Pval Abcd
Input was Abcd

£> Test-Pval 2bcd
Test-Pval : Cannot validate argument on parameter ‘parm1’. The argument “2bcd” does not match the “^[a-z | @,#]”
pattern. Supply an argument that matches “^[a-z | @,#]” and try the command again.
At line:1 char:11
+ Test-Pval 2bcd
+           ~~~~
+ CategoryInfo          : InvalidData: (:) [Test-Pval], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Test-Pval
£> Test-Pval ‘@bcd’
Input was @bcd

£> Test-Pval ‘#bcd’
Input was #bcd

£> Test-Pval ‘?bcd’
Test-Pval : Cannot validate argument on parameter ‘parm1’. The argument “?bcd” does not match the “^[a-z | @,#]”
pattern. Supply an argument that matches “^[a-z | @,#]” and try the command again.
At line:1 char:11
+ Test-Pval ‘?bcd’
+           ~~~~~~
+ CategoryInfo          : InvalidData: (:) [Test-Pval], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Test-Pval

Expect to spend a long time figuring out the regex though

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: