PowerShell的switch条件使用

测试取值范围

$x=8
switch($x){
 {$_ -gt 10} {"大于10"}
 10 {"等于10"}
 {$_ -lt 10} {"小于10"}
}

没有匹配

$x=8
switch($x){
 {$_ -gt 10} {"大于10"}
 10 {"等于10"}
 default {"没有匹配的"}
}

只匹配一次

$x=8
switch($x){
 {$_ -lt 8} {"小于8 或者 等于8";break}
 8 {"等于8"}
}

比较字符串

#str="xixihaha"
switch($str){
 "xixihaha" {"1"}
 "hahaxixi" {"2"}
}

大小写敏感

#str="xixihaha"
switch -case ($str){
 "XXIhaha" {"1"}
 "xixihaha" {"2"}
}

使用通配符

#str="www.baidu.com"
switch -wildcard($str){
 "*.com" {"匹配.com结尾"}
 "*.cn" {"匹配.cn结尾"}
 "*.cnn" {"匹配.cnn结尾"} 
}

猜你喜欢

转载自blog.csdn.net/weixin_42917630/article/details/94559317