I've been writing about a number of parameters attributes you can include in your PowerShell scripting to validate parameter values. Today I want to cover using a regular expression pattern to validate a parameter value. I'm going to assume you have a rudimentary knowledge of how to use regular expressions in PowerShell. If not, there is an entire chapter devoted to the topic in Windows PowerShell 2.0: TFM.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
The parameter attribute is [ValidatePattern()]. Inside the parentheses you place a scriptblock with the regular expression pattern. For example, in PowerShell we might write a command like this to verify if something is a number of 1 to 3 digits.:
$x -match "^\d{1,3}$"
To use that pattern in a [ValidatePattern()] attribute, you would write it like this:
[ValidatePattern({^\d{1,3}$})]
There is no need to use the -match operator or $_. Sure, I suppose you could write a validation script to achieve the same effect, but this is just as easy. I recommend testing your pattern from the PowerShell prompt, especially testing for failures. Here's a more complete example.
Param (
[Parameter(Position=0,Mandatory=$True,HelpMessage="Enter a UNC path like \\server\share")]
[ValidatePattern({^\\\\\S*\\\S*$})]
[ValidateScript({Test-Path -Path $_ })]
[string]$Path
)
Write-Host "Getting top level folder size for $Path" -ForegroundColor Magenta
dir $path | measure-object -Property Length -sum
For you regular expression gurus, don't get hung up on my pattern. It works for my purposes of illustration. Your pattern can be as simple or as complex as you need it to be. In this short script I'm expecting a path value like \\file01\public. If the value is not in this format, the pattern validation will fail, PowerShell will throw an exception and the script will fail.
Notice I'm also using a second parameter validation attribute, [ValidateScript()]. It is possible for the pattern to be correct but invalid so I can combine both validation tests.
PS C:\> S:\Demo-ValidatePattern.ps1 '\\file01\temp'
C:\scripts\Demo-ValidatePattern.ps1 : Cannot validate argument on parameter 'Pa
th'. The "Test-Path -Path $_ " validation script for the argument with value "\
\file01\temp" did not return true. Determine why the validation script failed a
nd then try the command again.
At line:1 char:28
+ S:\Demo-ValidatePattern.ps1 <<<< '\\file01\temp'
+ CategoryInfo : InvalidData: (:) [Demo-ValidatePattern.ps1], Par
ameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Demo-ValidatePa
ttern.ps1
If you'd like to try out my sample script, you can download it here.
hi
I think you can also use RegexOptions:
[enum]::GetNames([Text.RegularExpressions.RegexOptions])
Inside the parentheses you place a scriptblock with the regular expression pattern
you can also use You can also use quotes:
PS> gc function:foo
function foo {
Param (
[ValidatePattern('(?i)^(powershell)\1')]
$test
)
echo $test
}
PS> foo ('PoWerSHell'*2)
You should be able to use any pattern that would use with -match. If it works there, it should work here.