Powershell is a really powerful scripting language. It provides a lot of modern useful programming language features. It also have basic features like conditional statements like If
. In this tutorial we will look syntax and use cases of If .
Powershell是一种非常强大的脚本语言。 它提供了许多现代有用的编程语言功能。 它还具有基本功能,例如条件语句(如If
。 在本教程中,我们将研究If的语法和用例。
If is used to check if specified condition/s are met and if the condition is met do something if not check other conditions or exit. We will look different usage types below.
如果用于检查是否满足指定的条件,以及是否满足条件,请执行其他操作(如果不检查其他条件)或退出。 我们将在下面介绍不同的用法类型。
句法 (Syntax)
Generic syntax of the if
statement is like below. Other than first if
line each line optional. There may be more than one elseif
line
if
语句的通用语法如下所示。 除了第一if
行每行可选。 可能有不止一个elseif
线
If (condition) {Block Command}
elseIf (condition) {Block Command}
...
else {Block Command}
单一条件 (Single Condition)
We will start with simplest usage type of If. We will only check a single condition. According to conditions status which is logical values true
or false
specified script will run or not. Now we will do an example.
我们将从If的最简单用法类型开始。 我们将只检查一个条件。 根据条件状态(逻辑值true
或false
指定的脚本将运行或不运行。 现在我们将举一个例子。
In this example we will check if variable $a
is bigger than 5 . If $a is bigger than 5 then we will print a message saying that $a value is bigger than 5.
在此示例中,我们将检查变量$a
是否大于5。 如果$ a大于5,则我们将输出一条消息,指出$ a的值大于5。
$a=10
if ($a -gt 5){
Write-Host("$a is bigger then 5")
}
Now lets examine what this code mean.
现在让我们检查一下这段代码的含义。
$a=10
creates a numeric variable and assigns the value 10$a=10
创建一个数字变量并将值分配为10if ()
checks the statement between brackets and is the condition is true the code between curly brackets{ Write-Host ("$a is bigger then 5")}
is executed if not nothing will be executedif ()
检查方括号之间的语句,if ()
条件为true,则执行大括号之间的代码{ Write-Host ("$a is bigger then 5")}
如果不执行任何操作,将执行$a -gt 5
is the most important part -gt means greater then so if we try to read this statement variable a is greater then. This statement will be used as condition for if. In this example $a is 10 and this statement is true.$a -gt 5
是最重要的部分-gt意味着更大,因此,如果我们尝试读取此语句,则变量a更大。 该语句将用作if的条件。 在此示例中,$ a为10,该语句为true。
多种条件(Multiple Conditions)
Previous example we have check single condition. But the real world consist of complex problems and we have to use complex and multiple conditions in decision making process. If
can be used to provide multiple conditions by defining them in else
lines.
在前面的示例中,我们检查了单个条件。 但是现实世界包含复杂的问题,我们在决策过程中必须使用复杂的多种条件。 通过在else
行中定义If
可以提供多个条件。
In this example we will check variable $a
whether it is bigger than 10 or not.
在此示例中,我们将检查变量$a
是否大于10。
$a=5
if ($a -gt 10){
Write-Host("$a is bigger then 10")
}
elseif ($a -le 10){
Write-Host("$a is lower then 10")
}
The if statement execution will start with first
if
line but the variable data is 5 and not bigger than 10. So this line will be skipped.if语句的执行将从第一条
if
行开始,但是变量数据为5并且不大于10。因此将跳过此行。The
if
will resume withelseif
statement and as we see that the($a -le 10)
condition will return true.if
将使用elseif
语句恢复,并且我们看到($a -le 10)
条件将返回true。Because of the
elseif
condition is true the code block ofelseif
will be executed.因为
elseif
条件为true,否则将执行elseif
的代码块。

We can add more elseif
conditions than one and all of them will have same syntax but different conditions. Here an other example which have more than one elseif
我们可以添加更多的elseif
条件,而不是一个,并且所有条件将具有相同的语法,但条件不同。 这里,其具有多于一个的其它示例elseif
$a=5
if ($a -gt 10){
Write-Host("$a is bigger then 10")
}
elseif ($a -le 10){
Write-Host("$a is lower or equal to 10 and bigger than 0")
}
elseif ($a -lt 0){
Write-Host("$a is lower then 0")
}
其他 (Else)
Up to now we have defined our conditions explicitly by using if
or elseif
statements. What is we do not define the condition but if the previous conditions do not match we want to execute a code block? We call this else
statement. Else
is put as the last statement of the whole if
block. If none of the previous conditions are met the last else
code block is executed.
到目前为止,我们已经通过使用if
或elseif
语句明确定义了我们的条件。 我们没有定义条件是什么,但是如果先前条件不匹配,我们要执行代码块? 我们称此为else
声明。 Else
则作为整个if
块的最后一个语句。 如果先前的条件都不满足,则执行最后的else
代码块。
In the example if the variable $a
is not positive integer we will print a warning message to the console.
在示例中,如果变量$a
不是正整数,我们将向控制台输出警告消息。
$a=-4
if ($a -gt 10){
Write-Host("$a is bigger then 10")
}
elseif ($a -ge 0){
Write-Host("$a is lower or equal to 10 and bigger than 0")
}
else{
Write-Host("$a is not positive integer")
}

翻译自: https://www.poftut.com/powershell-if-elseif-else-conditional-statement/