關於 Switch

簡短描述

說明如何使用開關處理多個 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 必須至少包含一個 condition 語句。

當值不符合任何條件時,就會觸發 Default 子句。 它相當於 Else 語句中的 If 子句。 每個Default語句中只允許一個Switch子句。

Switch 具有下列參數:

參數 說明
通配符 指示條件為通配符字串。
如果 match 子句不是字串,則參數為
忽視。
精確 指示 match 子句(如果是字串)必須
完全匹配。 如果 match 子句不是字串,則此參數
被忽略。
區分大小寫 執行區分大小寫的匹配。 如果 match 子句不是
字串,則忽略此參數。
檔案 從檔而不是 value 語句中獲取 Input。 如果
包含多個 File 參數,只有最後一個是
使用。 檔的每一行都由
Switch 聲明。
正則表達式 執行值與
條件。 如果
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

若要讓 「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 = '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

另請參閱

關於_Break

關於_繼續

關於_如果

關於腳本區塊