PowerShell 技能连载 - 验证变量有效性

变量和函数参数可以通过验证属性自动地验证有效性。以下是一个简单的例子,确保 $test1 只能存储 1-10 之间的值:

1
[ValidateRange(1,10)]$test1 = 10

当您将一个小于 1 或大于 10 的值赋给这个变量,PowerShell 将会抛出一个异常。不过通过这种方式您无法控制异常的问本。

通过使用脚本验证器,您可以选择自己希望的错误信息:

1
2
3
4
5
6
7
8
[ValidateScript({
If ($_ -gt 10)
{ throw 'You have submitted a value greater than 10. That will not work, dummy!' }
Elseif ($_ -lt 1)
{ throw 'You have submitted a value lower than 1. That will not work, dummy!' }

$true
})]$test2 = 10

以下是输出结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
PS C:\> $test2 =  4

PS C:\> $test2 = 11
You have submitted a value greater than 10. That will not work, dummy!
At line:5 char:3
+ { throw 'You have submitted a value greater than 10. That will not work, dummy ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (You have submitted a...not work, dummy!:String
) [], RuntimeException
+ FullyQualifiedErrorId : You have submitted a value greater than 10. That will not work, dummy!



PS C:\> $test2 = -2
You have submitted a value lower than 1. That will not work, dummy!
At line:7 char:3
+ { throw 'You have submitted a value lower than 1. That will not work, dummy ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (You have submitted a...not work, dummy!:String
) [], RuntimeException
+ FullyQualifiedErrorId : You have submitted a value lower than 1. That will not work, dummy!

PowerShell 技能连载 - 验证变量有效性

http://blog.vichamp.com/2017/05/29/validating-variables/

作者

吴波

发布于

2017-05-29

更新于

2022-07-06

许可协议

评论