在 Windows PowerShell 中终止脚本
在 Windows PowerShell 中有多种终止脚本的方法。但是,其中一些在上下文方面可能听起来相似,但在功能方面,它们的实际目的却彼此不同。
本文将列举在 Windows PowerShell 中终止脚本的方法并一一定义。
在 Windows PowerShell 中使用 Exit
命令终止脚本
Exit
命令将退出从其名称派生的脚本。如果你没有打开的会话,此命令还将关闭你的 shell 或脚本窗口。Exit
命令还有助于通过使用退出代码提供反馈。
exit
仅运行 exit
命令可以具有退出代码 0
(默认),表示成功或正常终止,或 1
,表示失败或未捕获的抛出。
退出代码的优点是退出代码是完全可定制的。只要退出码是整数,退出码就有效。此外,要知道最后的退出代码,你可以输出变量 $LASTEXITCODE
。
退出.ps1
Write-Output 'Running sample script.'
exit 200
示例代码:
PS C:\>powershell.exe .\Exit.ps1
PS C:\>Running sample script.
PS C:\>Write-Output $LASTEXITCODE
PS C:\>200
另外,请记住,当你从正在运行的 PowerShell 脚本中使用 exit
命令调用另一个 PowerShell 文件时,正在运行的脚本也将终止。
在 Windows PowerShell 中使用 Throw
命令终止脚本
Throw
命令类似于带有退出代码的 Exit
命令,但信息量更大。你可以使用命令和自定义表达式来生成终止错误。通常,Throw
命令用于 Try-Catch
表达式内的 Catch
块内,以充分描述异常。
示例代码:
Try{
$divideAnswer = 1/0
}Catch{
Throw "The mathematical expression has a syntax error"
}
输出:
The mathematical expression has a syntax error
At line:4 char:5
+ Throw "The mathematical expression has a syntax error"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (The mathematica... a syntax error:String) [], RuntimeException
+ FullyQualifiedErrorId : The mathematical expression has a syntax error
Return
命令
与 Exit
命令不同,Return
命令将返回到其先前的调用点并且不会关闭你的脚本窗口。
通常,我们使用 Return
命令从脚本中某处执行的函数返回值。
示例代码:
Function sumValues($int1,$int2){
Return ($int1 + $int2)
}
# The function sumValues is called below, and the script will return to
# the same line with a value and store it in the output variable
$output = sumValues 1 2
Write-Output $output
输出:
3
break
命令
我们使用 break
命令来终止循环和案例。
示例代码:
$numList = 1,2,3,4,5,6,7,8
foreach($number in $numList){
if ($number -eq 8){
#Terminate loop if number variable is equal to 8
break
}
Write-Output $number
}
输出:
1
2
3
4
5
6
7
如果我们有嵌套循环,我们只会从调用 break
命令的循环中中断。
示例代码:
While ($true) {
While ($true) {
#The break command will only break out of this while loop
break
}
#The script will continue to run on this line after breaking out of the inner while loop
}
如果你想跳出一个特定的嵌套循环,break
命令使用标签作为它的参数。
While ($true) {
:thisLoop While ($true) {
While ($true) {
#The break command below will break out of the `thisLoop` while loop.
Break thisLoop
}
}
}
continue
命令
continue
命令还会在循环级别终止脚本。尽管如此,continue
命令并不会立即终止整个循环,而是仅终止当前迭代并保持循环继续进行,直到所有迭代都已处理完毕。
我们可以说这是一个在执行循环时跳过某些内容的命令。
示例代码:
$numList = 1,2,3,4,5,6,7,8
foreach($number in $numList){
if ($number -eq 2){
#The continue command below will skip number 2 and will directly process the next iteration, resetting the process to the top of the loop.
continue
}
Write-Output $number
}
输出:
1
3
4
5
6
7
8
相关文章
在 Windows PowerShell 中获取命令行参数
发布时间:2024/03/04 浏览次数:358 分类:编程语言
-
本文将解释我们如何使用 PowerShell 的参数函数处理命令行参数,参数如何工作,我们如何使用 PowerShell 参数将值传递给参数,以及定义参数的基本方法是什么。
Windows PowerShell 中的 Base64 编码
发布时间:2024/03/04 浏览次数:332 分类:编程语言
-
本文将展示如何编码和解码 base64 字符串。Windows PowerShell 当前版本没有本机命令,因此我们将向你展示如何执行此操作的替代方法。
在 Windows PowerShell 中写入输出
发布时间:2024/03/04 浏览次数:259 分类:编程语言
-
本文将向你展示如何在 Windows PowerShell 中编写或打印输出。本文还将区分多个 write cmdlet 并解释它们的意义。
在 Windows PowerShell 中设置 PATH 环境变量
发布时间:2024/03/04 浏览次数:350 分类:编程语言
-
本文将展示在 Windows PowerShell 中设置路径环境变量的正确方法。本文还展示了如何在持久性和非持久性方法中设置变量。
在 Windows PowerShell 中打印环境变量
发布时间:2024/03/04 浏览次数:869 分类:编程语言
-
本文将讨论什么是环境变量以及如何使用多个 Windows PowerShell 命令打印它们。
在 PowerShell 中注释代码
发布时间:2024/03/04 浏览次数:363 分类:编程语言
-
本文将展示在 Windows PowerShell 中注释代码的多种方法。本文还介绍了注释代码时的一些最佳实践。
在 PowerShell 中执行 LDAP 查询
发布时间:2024/03/04 浏览次数:304 分类:编程语言
-
本文将深入了解如何使用 Active Directory 过滤器和 LDAP 过滤器。