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

[玩转系统] PowerShell 中的 If 语句 | PowerShell 中 If 语句的完整指南

作者:精品下载站 日期:2024-12-14 04:53:53 浏览:14 分类:玩电脑

PowerShell 中的 If 语句 | PowerShell 中 If 语句的完整指南


[玩转系统] PowerShell 中的 If 语句 | PowerShell 中 If 语句的完整指南

PowerShell 中的 If 语句简介

PowerShell 中的 If 语句允许程序员控制程序的执行流程,“IF”语句根据给定的条件表达式是否正确来定义程序是否必须执行一段代码。这里的编程术语正确或错误。 if 语句的主要用途之一允许程序根据一个或多个条件做出决定。

如果语句基于布尔值,则如果布尔条件值为 true,则在 if 块内执行 if 块内的语句行。换句话说,要执行任何语句,它必须测试一个或多个条件,如果条件为真,它将执行该语句。当我们想要检查任何特定情况时需要“如果”语句。

[玩转系统] PowerShell 中的 If 语句 | PowerShell 中 If 语句的完整指南

什么是 PowerShell?

PowerShell 是一种在 Windows 环境中编写管理命令的方法,类似于 Linux 中的 bash 脚本。它提供了一种自动化 Windows 操作系统及其应用程序来处理各种任务的方法。PowerShell 可用于 Windows 和 Linux 操作系统,但在本教程中,我们重点关注 Windows。在下图中,我们可以看到 Powershell 如何控制所有自动化工作。

语法:

PowerShell 中 if 的语法与其他编程语言非常相似。它检查条件,如果条件表达式为 true,则转到 if 块,如果条件表达式为 false,则转到 else。

if(condition) {
// Executes when the condition is true
}else {
// Executes when the condition is false
}

我们还可以使用下面的 elseif, 语法。

if(condition 1) {
// Executes when the condition 1 is true
}elseif(condition 2) {
// Executes when the condition 2 is true
}elseif(condition 3) {
// Executes when the condition 3 is true
}else {
// Executes none of the condition is true.
}

流程图

[玩转系统] PowerShell 中的 If 语句 | PowerShell 中 If 语句的完整指南

在下面的流程图中,我们可以看到当执行开始时,它首先检查条件。如果条件为真,那么它将转到语句块。这里的条件可以是一个,也可以是多个。除零、假、空白之外的任何条件仅被视为真。例如,如果任何条件表达式给出输出 0、”、假,则所有这些都被视为假语句。

PowerShell 中的 if 语句如何工作?

if (<cond1>)
{<statement1>}
[elseif (<cond2>)
{<statement2>}]
[else
{<statement3>}]

在这里,当它开始执行时,它会检查 cond1 是否为 true 或 false,根据该值,它将执行语句块,如果 cond1 为 true,它将执行 statements1 并退出 PowerShell。但如果 cond1 为 false,那么它将检查 else if 块 cond2,如果 cond2 为 true,则将执行 statements2。如果 cond1 和 cond2 都为 false 或没有一个条件为 true,则将执行 else 语句。

例如,条件可以是一个或多个。

if (<condition 1 -or condition 2>)
{<statement1>}
[elseif (<condition 3 -or condition 4>)
{<statement2>}]
[else
{<statement3>}]

PowerShell 中的 If 语句示例

PowerShell 中的 If 语句示例如下:

示例 #1 - 简单的 if-else 示例

代码:

$x = 40
if($x -le 20){
write-host("value of x is less than 20")
}else{
write-host(“value of x is greater than 20”)
}

输出:

[玩转系统] PowerShell 中的 If 语句 | PowerShell 中 If 语句的完整指南

说明:上面的代码检查$x的值,是否小于20,如果$x的值小于20,则语句块将执行。

示例 #2 - 具有多个条件

代码:

$day = (get-date).dayofweek
if(($day -ne "Saturday") -or ($day -ne "Sunday")){
write-host("Welcome to Our Banks")
}else{
write-host(“Hello friends , Banks are closed today”)
}

输出:

[玩转系统] PowerShell 中的 If 语句 | PowerShell 中 If 语句的完整指南

说明:

上面的代码将根据今天打印输出。在这里,它与 $day 的值匹配,因此如果某一天是星期日或星期六,它将打印“你好朋友,银行今天休息”,如果不是星期日和星期六,它将打印“欢迎来到我们的银行”。

示例 #3 - if-else if 条件

代码:

$occupation =”engineering”
if($occupation -eq "engineering"){
write-host("engineer")
}elseif($occupation -eq "sales"){
write-host("sales")
}else{
write-host("accounting")
}

输出:

[玩转系统] PowerShell 中的 If 语句 | PowerShell 中 If 语句的完整指南

说明:

在上面的代码中,它检查$ocupation值,如果它等于engineering而不是打印engineering,如果该值是sales,那么它将打印sales,如果$occupation是none,那么它将打印accounting。

示例 #4 - 基于功能

代码:

function check ($VALUE) {
if ($VALUE) {
Write-Host(“TRUE”)
} else {
Write-Host(“FALSE”)
}
}
check $FALSE
check $TRUE
check FALSE //FALSE is a String with length > 0
check TRUE //TRUE is a string length >0

输出:

[玩转系统] PowerShell 中的 If 语句 | PowerShell 中 If 语句的完整指南

说明:

在上面的例子中,首先,它调用带有参数 $TRUE 的 check 函数,在函数 check 中,它检查 $VALUE ,如果它是 true ,它将打印 TRUE ,以类似的方式再次使用参数 $FALSE 调用 check ,它检查 $VALUE ,因为它是 false ,所以它将转到 else 块。

让我向您解释一些我们需要“if”的实际用例。

如果我们想检查数据库服务器是启动还是关闭(检查数据库是否正在运行),那么我们可以使用 if 语句。因此,如果数据库已关闭,则运行任何服务或命令来启动数据库。这将是完全自动化的,它将始终检查数据库状态。

代码:

$x = Dbconnection()//$x is true or false
if($x){
write-host("Start database services process")
//run some script to handle a situation
}

很多时候,由于服务器负载过重,它停止工作,因此要检查服务器的阈值负载能力,我们可以使用 Powershell if 条件和内部条件,我们可以编写所需的语句。

代码:

$x = loadValue()//200000 is threshold load capacity
if($x -le 200000){
write-host("Load is ok")
}else{
write-host("Load is exceed threshold capacity")
//write conditions for handling conditions .
}

通过上面的例子,我们清楚了一个语句是否能够在现实的软件世界中起到非常关键的作用。

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

取消回复欢迎 发表评论:

关灯