关于开关
简短说明
说明如何使用 开关来处理多个 If
语句。
长说明
若要在脚本或函数中检查条件,请使用 If
语句。 语句If
可以检查多种类型的条件,包括变量的值和 对象的属性。
若要检查多个条件,请使用 Switch
语句。 语句 Switch
等效于一系列 If
语句,但更简单。 语句 Switch
列出每个条件和可选操作。 如果条件获得,则执行该操作。
语句 Switch
可以使用 $_
和 $switch
自动变量。 有关详细信息,请参阅 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
的行为与使用 Exact 参数的行为相同。 它对值执行不区分大小写的匹配。 如果值是集合,则按其出现顺序计算每个元素。
语句 Switch
必须至少包含一个条件语句。
Default
当值与任何条件都不匹配时,将触发 子句。 它等效于 Else
语句中的 If
子句。 每个Switch
语句中只允许一个Default
子句。
Switch
具有以下参数:
- 通配符 - 指示条件是通配符字符串。 如果 match 子句不是字符串,则忽略 参数。 比较不区分大小写。
- Exact - 指示 match 子句(如果是字符串)必须完全匹配。 如果 match 子句不是字符串,则忽略此参数。 比较不区分大小写。
- CaseSensitive - 执行区分大小写的匹配。 如果 match 子句不是字符串,则忽略此参数。
- File - 从文件而不是值语句获取输入。 如果包含多个 File 参数,则仅使用最后一个参数。 由 语句读取和计算
Switch
文件的每一行。 比较不区分大小写。 - Regex - 执行值与条件的正则表达式匹配。 如果 match 子句不是字符串,则忽略此参数。
比较不区分大小写。 自动
$matches
变量可用于匹配的语句块中。
注意
指定冲突值(如 正则表达式 和 通配符)时,指定的最后一个参数优先,并且忽略所有冲突参数。 还允许多个参数实例。 但是,只有使用的最后一个参数才有效。
在此示例中,将不是字符串或数字数据的对象传递给 Switch
。 对 Switch
对象执行字符串强制,并评估结果。
$test = @{
Test = 'test'
Test2 = 'test2'
}
$test.ToString()
switch -Exact ($test)
{
'System.Collections.Hashtable'
{
'Hashtable string coercion'
}
'test'
{
'Hashtable value'
}
}
System.Collections.Hashtable
Hashtable string coercion
在此示例中,没有匹配大小写,因此没有输出。
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
若要使单词“fourteen”与大小写匹配, -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 = 'https://bing.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 that uses $($matches[1])"; Break }
}
https://bing.com is a web address that uses https
Switch 语句条件可以是:
- 其值与输入值进行比较的表达式
- 满足条件时应返回
$true
的脚本块。
自动 $_
变量包含传递给 switch 语句的值,可在条件语句的范围内进行评估和使用。
每个条件的操作独立于其他条件中的操作。
以下示例演示如何使用脚本块作为 Switch
语句条件。
switch ("Test")
{
{$_ -is [String]} {
"Found a string"
}
"Test" {
"This $_ executes as well"
}
}
Found a string
This Test executes as well
如果值与多个条件匹配,则执行每个条件的操作。 若要更改此行为,请使用 Break
或 Continue
关键字。
Break
关键字 (keyword) 停止处理并退出Switch
语句。
关键字 (keyword) Continue
停止处理当前值,但继续处理任何后续值。
以下示例处理一个数字数组,并显示它们是否为奇数或偶数。 使用关键字 (keyword) 跳过Continue
负数。 如果遇到非数字,则使用 关键字 (keyword) 终止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