简短说明
说明如何使用开关处理多个 If 语句。
详细说明
若要检查脚本或函数中的条件,请使用 If
语句。
If
语句可以检查许多类型的条件,包括变量的值和对象的属性。
若要检查多个条件,请使用 Switch
语句。 该 Switch
语句等效于一系列 If 语句,但更简单。
Switch
语句列出了每个条件和可选操作。 如果条件成立,则执行该操作。
该 Switch
语句还使用 $switch
automatic 变量。 有关详细信息,请参阅 about_Automatic_Variables。
基本 Switch
语句具有以下格式:
Switch (<test-value>)
{
<condition> {<action>}
<condition> {<action>}
}
例如,以下 Switch
语句将测试值 3 与每个条件进行比较。 当测试值与条件匹配时,将执行该作。
switch (3)
{
1 {"It is one."}
2 {"It is two."}
3 {"It is three."}
4 {"It is four."}
}
It is three.
在此简单示例中,该值与列表中的每个条件进行比较,即使值 3 匹配。 以下 Switch
语句的值为 3 的两个条件。 它演示默认情况下会测试所有条件。
switch (3)
{
1 {"It is one."}
2 {"It is two."}
3 {"It is three."}
4 {"It is four."}
3 {"Three again."}
}
It is three.
Three again.
若要指示 Switch
在匹配后停止比较,请使用 Break
语句。
Break
语句终止 Switch
语句。
switch (3)
{
1 {"It is one."}
2 {"It is two."}
3 {"It is three."; Break}
4 {"It is four."}
3 {"Three again."}
}
It is three.
如果测试值是一个集合(如数组),则会按其出现的顺序对集合中的每个项进行求值。 以下示例依次计算 4 和 2。
switch (4, 2)
{
1 {"It is one." }
2 {"It is two." }
3 {"It is three." }
4 {"It is four." }
3 {"Three again."}
}
It is four.
It is two.
任何 Break
语句都应用于该集合,而不是每个值,如以下示例所示。
Switch
语句由值为 4 中的条件中的 Break
语句终止。
switch (4, 2)
{
1 {"It is one."; Break}
2 {"It is two." ; Break }
3 {"It is three." ; Break }
4 {"It is four." ; Break }
3 {"Three again."}
}
It is four.
语法
完整的 Switch
语句语法如下所示:
switch [-regex|-wildcard|-exact][-casesensitive] (<value>)
{
"string"|number|variable|{ expression } { statementlist }
default { statementlist }
}
或
switch [-regex|-wildcard|-exact][-casesensitive] -file filename
{
"string"|number|variable|{ expression } { statementlist }
default { statementlist }
}
如果未使用任何参数, Switch
则对值执行不区分大小写的精确匹配。 如果值为集合,则每个元素的计算顺序显示。
Switch
语句必须至少包含一个条件语句。
当值与任何条件不匹配时,将触发 Default
子句。 它等效于 Else
语句中的 If
子句。 每个 Default
语句中只允许一个 Switch
子句。
Switch
具有以下参数:
参数 | DESCRIPTION |
---|---|
通配符 | 指示条件为通配符字符串。 |
如果 match 子句不是字符串,则参数为 | |
忽视。 | |
确切 | 指示 match 子句(如果是字符串)必须 |
完全匹配。 如果 match 子句不是字符串,则此参数 | |
被忽略。 | |
区分大小写 | 执行区分大小写的匹配。 如果 match 子句不是 |
字符串,则忽略此参数。 | |
文件 | 从文件而不是 value 语句中获取 Input。 如果 |
包含多个 File 参数,只有最后一个是 | |
使用。 文件的每一行都由 | |
Switch 陈述。 |
|
Regex | 执行值与 |
condition。 如果 | |
match 子句不是字符串,则忽略此参数。 |
注释
指定冲突值(如 正则表达式 和 通配符)时,指定的最后一个参数优先,并忽略所有冲突参数。 还允许多个参数实例。 但是,只有最后一个使用的参数有效。
在此示例中,没有匹配的情况,因此没有输出。
switch ("fourteen")
{
1 {"It is one."; Break}
2 {"It is two."; Break}
3 {"It is three."; Break}
4 {"It is four."; Break}
"fo*" {"That's too many."}
}
通过添加 Default
子句,可以在没有其他条件成功时执行作。
switch ("fourteen")
{
1 {"It is one."; Break}
2 {"It is two."; Break}
3 {"It is three."; Break}
4 {"It is four."; Break}
"fo*" {"That's too many."}
Default {
"No matches"
}
}
No matches
若要使单词“14en”与大小写匹配,必须使用 -Wildcard
或 -Regex
参数。
PS> switch -Wildcard ("fourteen")
{
1 {"It is one."; Break}
2 {"It is two."; Break}
3 {"It is three."; Break}
4 {"It is four."; Break}
"fo*" {"That's too many."}
}
That's too many.
以下示例使用 -Regex
参数。
$target = 'user@contoso.com'
switch -Regex ($target)
{
'ftp\://.*' { "$_ is an ftp address"; Break }
'\w+@\w+\.com|edu|org' { "$_ is an email address"; Break }
'http[s]?\://.*' { "$_ is a web address"; Break }
}
user@contoso.com is an email address
Switch
语句条件可以是:
- 其值与输入值进行比较的表达式
- 一个脚本块,如果满足条件,则应返回
$true
该脚本块。 脚本块接收当前对象以在 automatic 变量中$_
进行比较,并在其自己的范围内进行评估。
每个条件的作独立于其他条件中的作。
以下示例演示如何将脚本块用作 Switch
语句条件。
switch ("Test")
{
{$_ -is [String]} {
"Found a string"
}
"Test" {
"This executes as well"
}
}
Found a string
This executes as well
如果值与多个条件匹配,则执行每个条件的作。 若要更改此行为,请使用 Break
或 Continue
关键字。
Break
关键字停止处理并退出 Switch
语句。
Continue
关键字停止处理当前值,但继续处理任何后续值。
下面的示例处理一组数字,并显示它们是否为奇数或偶数。 使用 Continue
关键字跳过负数。 如果遇到非数字,则使用 Break
关键字终止执行。
switch (1,4,-1,3,"Hello",2,1)
{
{$_ -lt 0} { Continue }
{$_ -isnot [Int32]} { Break }
{$_ % 2} {
"$_ is Odd"
}
{-not ($_ % 2)} {
"$_ is Even"
}
}
1 is Odd
4 is Even
3 is Odd