簡短描述
說明如何使用參數來處理多個 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語句會由 Break 值4的條件中的語句終止。
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.
Syntax
完整的 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具有下列參數:
| 參數 | 描述 |
|---|---|
| 模糊 | 指出條件為萬用字元字串。 |
| 如果 match 子句不是字串,參數會是 | |
| 忽略. | |
| 精確 | 指出 match 子句(如果它是字串)必須 |
| 完全相符。 如果 match 子句不是字串,則此參數 | |
| 會被忽略。 | |
| CaseSensitive | 執行區分大小寫的比對。 如果 match 子句不是 |
| 字串,此參數會被忽略。 | |
| 檔案 | 接受來自檔案的輸入,而不是值語句。 If |
| 包含多個檔案參數,只有最後一個是 | |
| 作為. 檔案的每一行都會由 | |
Switch句. |
|
| Regex | 執行與值的正則運算式比對。 |
| 條件. 如果裝置上無法使用 | |
| match 子句不是字串,此參數會被忽略。 |
注意
指定衝突的值(例如Regex和萬用字元)時,會優先使用指定的最後一個參數,而且會忽略所有衝突的參數。 也允許多個參數實例。 不過,只有最後使用的參數才有效。
在此範例中,沒有任何相符的大小寫,因此沒有輸出。
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
若要讓 "十四" 這個字元合案例,您必須使用 -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。 腳本區塊會接收要在自動變數中進行比較的目前物件$_,並在其本身的範圍中進行評估。
每個條件的動作與其他條件中的動作無關。
下列範例示範如何使用腳本區塊做為 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