forked from jdhitsolutions/ISEScriptingGeek
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabout_Scripting_ValidatePattern.help.txt
More file actions
77 lines (60 loc) · 6.39 KB
/
about_Scripting_ValidatePattern.help.txt
File metadata and controls
77 lines (60 loc) · 6.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
TOPIC
about_Scripting_ValidatePattern
SHORT DESCRIPTION
A tutorial on validating a parameter value based on a regular expression
pattern match.
LONG DESCRIPTION
This topic will 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 by Don Jones
and Jeffery Hicks (SAPIEN Press).
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 Red
dir $path | measure-object -Property Length -sum
For you regular expression gurus, don’t get hung up on my pattern. It
works for my illustration purposes. 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:\Scripts\> .\Demo-ValidatePattern.ps1 '\\file01\temp'
C:\scripts\Demo-ValidatePattern.ps1 : Cannot validate argument on para
meter 'Path'. The "Test-Path -Path $_ " validation script for the argu
ment with value "\\file01\temp" did not return true. Determine why the
validation script failed and then try the command again.
At line:1 char:28
+ C:\Scripts\Demo-ValidatePattern.ps1 <<<< '\\file01\temp'
+ CategoryInfo : InvalidData: (:) [Demo-ValidatePattern.ps
1], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Demo-Vali
datePattern.ps1
The bottom line is that any regular expression pattern you can use with
the -match operator should work with this validation attribute.
SEE ALSO
about_Functions_Advanced_Parameters
about_Regular_Expressions
about_Comparison_Operators
http://jdhitsolutions.com/blog