关于 IfAbout If

简短说明SHORT DESCRIPTION

描述可用于根据一个或多个条件测试的结果运行语句列表的语言命令。Describes a language command you can use to run statement lists based on the results of one or more conditional tests.

详细说明LONG DESCRIPTION

If如果指定的条件测试的计算结果为 true,则可以使用语句来运行代码块。You can use the If statement to run code blocks if a specified conditional test evaluates to true. 如果所有之前的测试的计算结果都为 false,则还可以指定要运行的一个或多个附加条件测试。You can also specify one or more additional conditional tests to run if all the prior tests evaluate to false. 最后,你可以指定一个其他代码块,如果没有其他任何之前的条件测试计算为 true,则会运行该代码块。Finally, you can specify an additional code block that is run if no other prior conditional test evaluates to true.

语法Syntax

下面的示例显示了 If 语句语法:The following example shows the If statement syntax:

if (<test1>)
    {<statement list 1>}
[elseif (<test2>)
    {<statement list 2>}]
[else
    {<statement list 3>}]

运行 If 语句时,PowerShell 会将 <test1> 条件表达式计算为 true 或 false。When you run an If statement, PowerShell evaluates the <test1> conditional expression as true or false. 如果 <test1> 为 true,则 <statement list 1> 运行,并且 PowerShell 将退出 If 语句。If <test1> is true, <statement list 1> runs, and PowerShell exits the If statement. 如果 <test1> 为 false,则 PowerShell 将计算由条件语句指定的条件 <test2>If <test1> is false, PowerShell evaluates the condition specified by the <test2> conditional statement.

如果 <test2> 为 true,则 <statement list 2> 运行,并且 PowerShell 将退出 If 语句。If <test2> is true, <statement list 2> runs, and PowerShell exits the If statement. 如果 <test1><test2> 的计算结果都为 false,则 <statement list 3> 代码块运行,并且 PowerShell 将退出 If 语句。If both <test1> and <test2> evaluate to false, the <statement list 3> code block runs, and PowerShell exits the If statement.

可以使用多个 Elseif 语句来链接一系列条件测试。You can use multiple Elseif statements to chain a series of conditional tests. 因此,仅当前面的所有测试都为 false 时,才会运行每个测试。So, that each test is run only if all the previous tests are false. 如果需要创建 If 包含多个 Elseif 语句的语句,请考虑改为使用 Switch 语句。If you need to create an If statement that contains many Elseif statements, consider using a Switch statement instead.

示例:Examples:

最简单的 If 语句包含一个命令,不包含任何 Elseif 语句或任何 Else 语句。The simplest If statement contains a single command and does not contain any Elseif statements or any Else statements. 下面的示例显示了语句的最简单形式 IfThe following example shows the simplest form of the If statement:

if ($a -gt 2) {
    Write-Host "The value $a is greater than 2."
}

在此示例中,如果 $a 变量大于2,则条件的计算结果为 true,并且语句列表将运行。In this example, if the $a variable is greater than 2, the condition evaluates to true, and the statement list runs. 但是,如果 $a 小于或等于2或者不是现有的变量,则该语句不 If 显示消息。However, if $a is less than or equal to 2 or is not an existing variable, the If statement does not display a message.

通过添加 Else 语句,当 $a 小于或等于2时,将显示一条消息。By adding an Else statement, a message is displayed when $a is less than or equal to 2. 如下面的示例所示:As the next example shows:

if ($a -gt 2) {
    Write-Host "The value $a is greater than 2."
}
else {
    Write-Host ("The value $a is less than or equal to 2," +
        " is not created or is not initialized.")
}

若要进一步优化此示例,可以使用 Elseif 语句在 $a 的值等于2时显示一条消息。To further refine this example, you can use the Elseif statement to display a message when the value of $a is equal to 2. 如下面的示例所示:As the next example shows:

if ($a -gt 2) {
    Write-Host "The value $a is greater than 2."
}
elseif ($a -eq 2) {
    Write-Host "The value $a is equal to 2."
}
else {
    Write-Host ("The value $a is less than 2 or" +
        " was not created or initialized.")
}

使用三元运算符语法Using the ternary operator syntax

PowerShell 7.0 使用三元运算符引入了新的语法。PowerShell 7.0 introduced a new syntax using the ternary operator. 它遵循 c # 三元运算符语法:It follows the C# ternary operator syntax:

<condition> ? <if-true> : <if-false>

三元运算符的行为类似于简化 if-else 语句。The ternary operator behaves like the simplified if-else statement. <condition>计算表达式,并将结果转换为布尔值,以确定下一次应计算哪个分支:The <condition> expression is evaluated and the result is converted to a boolean to determine which branch should be evaluated next:

  • 如果 <condition> 表达式为 true,则执行 <if-true> 表达式The <if-true> expression is executed if the <condition> expression is true
  • 如果 <condition> 表达式为 false,则执行 <if-false> 表达式The <if-false> expression is executed if the <condition> expression is false

例如:For example:

$message = (Test-Path $path) ? "Path exists" : "Path not found"

在此示例中,的值在 $message 返回时为 "Path exists" Test-Path $trueIn this example, the value of $message is "Path exists" when Test-Path returns $true. Test-Path 返回时 $false ,的值 $message 为 "找不到路径"。When Test-Path returns $false, the value of $message is "Path not found".

$service = Get-Service BITS
$service.Status -eq 'Running' ? (Stop-Service $service) : (Start-Service $service)

在此示例中,如果服务正在运行,它将停止,如果其状态为 "未在 运行 ",则启动该服务。In this example, if the service is running, it is stopped, and if its status is not Running , it is started.

另请参阅SEE ALSO

about_Comparison_Operatorsabout_Comparison_Operators

about_Switchabout_Switch