当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 如何使用 PowerShell Switch 语句

作者:精品下载站 日期:2024-12-14 18:43:30 浏览:13 分类:玩电脑

如何使用 PowerShell Switch 语句


PowerShell Switch 语句允许您针对多种条件进行测试。您可以将 switch 语句与多个 if 语句进行比较,只是以一种更简单、更清晰的方式。

大多数人不知道的是 switch 语句并不局限于单一条件。您实际上可以同时执行多个条件并事件测试多个值。使用 switch 语句优于使用多个 If 语句,因为它使代码更易于阅读和维护。

在本文中,我们将了解如何在 PowerShell 中使用 Switch 语句以及如何充分利用它。

使用 PowerShell Switch 语句

我们先来看看PowerShell Switch语句的基本用法。 switch 语句将一个值与多个条件进行比较。如果满足条件,则执行脚本块中的代码。

在下面的示例中,我们有一个变量 $car。 switch 语句将检查变量 car 是否符合条件之一。当条件满足时,脚本块将被执行,在这种情况下,它将向控制台写入一个字符串。如果未找到匹配项,则执行默认条件。

$car = "Toyota"

switch ($car) {
    "Toyota" {
        Write-Host "The car is a Toyota."
    }
    "Honda" {
        Write-Host "The car is a Honda."
    }
    "Ford" {
        Write-Host "The car is a Ford."
    }
    default {
        Write-Host "The car is not recognized."
    }
}

# Result
The car is a Toyota.

条件不限于字符串或数字,我们还可以在条件中使用比较。例如,如果我们想检查一个数字是否小于特定值:

$value = 5

switch ($value) {
    {$_ -le 10} {
        Write-Host "The input is lower then 10"
    }
    {$_ -le 20} {
        Write-Host "The input is lower then 20"
    }
}

# Result
The input is lower then 10
The input is lower then 20

现在,如果您运行上面的代码,您会注意到这两个条件都被执行了。这是正确的,因为值 5 不仅低于 10(第一个条件),而且低于 20。为了防止执行这两个条件,我们可以使用 break 语句。这样,如果找到匹配项,switch 语句将停止测试条件:

$value = 5

switch ($value) {
    {$_ -le 10} {
        Write-Host "The input is lower then 10";
        Break
    }
    {$_ -le 20} {
        Write-Host "The input is lower then 20";
        Break
    }
}

# Result
The input is lower then 10

切换多个值

通过 PowerShell 中的 switch 语句,我们还可以测试多个值。为此,您需要用逗号分隔这些值。然后将根据 switch 语句内的条件测试每个值。

例如,我们有一个水果列表,我们想知道我们有哪些水果:

$fruit = "grape","kiwi","pear"

switch ($fruit) {
    "apple" { Write-Host "We have an apple." }
    "orange" { Write-Host "We have an orange." }
    "banana" { Write-Host "We have a banana." }
    "pear" { Write-Host "We have a pear." }
    "strawberry" { Write-Host "We have a strawberry." }
    "grape" { Write-Host "We have a grape." }
    "mango" { Write-Host "We have a mango." }
    "kiwi" { Write-Host "We have a kiwi." }
    "pineapple" { Write-Host "We have a pineapple." }
    "watermelon" { Write-Host "We have a watermelon." }
    default { Write-Host "The fruit is not recognized." }
}

# Result
We have a grape.
We have a kiwi.
We have a pear.

PowerShell 开关参数

PowerShell Switch 语句带有几个参数,我们可以使用它们来确定如何测试该值:

  • 通配符 - 您可以在条件中使用通配符,例如*?

  • CaseSensative - 表示条件区分大小写。默认情况下,条件不区分大小写

  • 精确 - 默认启用。 switch 语句将始终执行精确比较,除非您使用通配符参数

  • Regex - 表示每个条件都被视为正则表达式。

  • 文件 - 允许您使用文件作为值。通过这种方式,您可以检查文件的内容是否符合其中一个条件。

通配符

Wildcard 参数允许您在条件中使用通配符。这样,只有字符串的一部分必须与值匹配,而不是完全匹配。您可以使用 * 检查字符串是否以指定字符开头或结尾。而 ? 用于替换单个字符:

$name = "John"

switch -Wildcard ($name) {
    "Jo*" {
        Write-Host "The name starts with 'Jo'."
    }
    "J?hn" {
        Write-Host "The name looks like John."
    }
    "J*o*" {
        Write-Host "The name contains 'J' and 'o'."
    }
    default {
        Write-Host "The name does not match any pattern."
    }
}

CaseSensative

默认情况下,PowerShell Switch 语句不区分大小写。但是,当您添加 casesensative 参数时,值应该以区分大小写的方式进行匹配:

$language = "PowerShell"

switch -CaseSensitive ($language) {
    "powershell" {
        Write-Host "The language is PowerShell in lower case."
    }
    "PowerShell" {
        Write-Host "The language is PowerShell in mixed case."
    }
    "PYTHON" {
        Write-Host "The language is Python in upper case."
    }
    default {
        Write-Host "The language is not recognized."
    }
}

正则表达式

正则表达式是检查字符串是否与特定模式匹配的好方法。创建正则表达式有时可能有点具有挑战性,但它们仍然非常强大。当您在 switch 语句中指定 regex 参数时,每个条件都被视为正则表达式字符串。

在下面的示例中,我们检查输入是否是有效的电子邮件地址、域名或电话号码:

$input = "[email "

switch -Regex ($input) {
    "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$" {
        Write-Host "The input is a valid email address."
    }
    "^[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$" {
        Write-Host "The input is a valid domain name."
    }
    "^\+?[0-9]{1,3}\s?[0-9]{7,14}$" {
        Write-Host "The input is a valid phone number."
    }
    default {
        Write-Host "The input is not recognized."
    }
}

文件

一个不太为人所知但仍然非常强大的选项是 Switch 语句中的文件参数。 file参数表示该值是一个文件。例如,我们可以通过这种方式检查文件的内容是否符合以下条件之一:

$file = "C:\Documents\example.txt"

switch -File $file {
    {$_ -match "apple"} {
        Write-Host "The file contains the word 'apple'."
    }
    {$_ -match "banana"} {
        Write-Host "The file contains the word 'banana'."
    }
    {$_ -match "orange"} {
        Write-Host "The file contains the word 'orange'."
    }
    default {
        Write-Host "The file does not contain any recognized words."
    }
}

例如,另一种选择是循环遍历包含文件的目录。然后,我们可以检查每个文件是否与条件中指定的文件类型之一匹配:

$path = "C:\Documents"

Get-ChildItem $path | ForEach-Object {
    switch -File ($_.FullName) {
        "*.txt" {
            Write-Host "$($_.Name) is a text file."
        }
        "*.docx" {
            Write-Host "$($_.Name) is a Word document."
        }
        "*.xlsx" {
            Write-Host "$($_.Name) is an Excel workbook."
        }
        default {
            Write-Host "$($_.Name) is not a recognized file type."
        }
    }
}

总结

当您需要在脚本中使用多个 if 语句时,通常最好使用 PowerShell Switch 语句。它允许您轻松地将单个或多个值与多个条件进行比较。

我希望这篇文章能让您更好地理解 switch 语句。如果您有任何疑问,请在下面发表评论。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯