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

[玩转系统] PowerShell 中的 Try-catch |在 PowerShell 中使用 Try-catch 的指南

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

PowerShell 中的 Try-catch |在 PowerShell 中使用 Try-catch 的指南


[玩转系统] PowerShell 中的 Try-catch |在 PowerShell 中使用 Try-catch 的指南

PowerShell 中的 Try-catch 简介

处理任何脚本或程序时的一个好习惯是拥有错误处理机制。尽管可能需要一些额外的时间,但其回报是无价的。在本文中的 PowerShell 中进行 Try-catch 时,任何脚本或程序都不可能 100% 地运行而不出现任何错误或问题。由于脚本中的逻辑错误,不一定总是会发生错误。许多外部因素也可能影响脚本的成功运行。为了避免此类意外,最好实现错误处理机制。

脚本执行期间可能会发生两种类型的错误。它们是终止错误和非终止错误。顾名思义,终止错误将阻止程序进一步执行,而非终止错误不会停止执行。终止错误的一个例子是语法错误,而非终止错误的一个例子是缺少文件。

了解 Try-Catch

在PowerShell中,错误处理是通过try和catch块完成的。 try 块将包含可能引发错误的代码。 catch 块包含在 try 块抛出错误时要执行的代码或操作。此外,最后一个块可用于释放资源。大多数情况下,非终止错误无法在 PowerShell 中处理。为了处理此类错误,需要将它们专门转换为终止错误。这可以使用 ErrorActionPreference 来完成。

语法:

try-catch块的一般语法如下

Try
{
//code1 that may generate exception
//code2 that may generate exception
//code3 that may generate exception
//to catch non-terminating error, convert them to terminating error
}
Catch(error)
{
//code to be executed
//multiple catch blocks can be included, or the same catch block can be used to catch multiple exceptions
// the error information is present inside the $_ variable
//The error can be logged to a text file or can trigger an email to inform some user about the error
}
Finally
{
//code to be executed
//this is an optional block of code
}

多重捕获的语法:

try
{
//code to be executed
}
catch [System.IO.DirectoryNotFoundException]
{
Write-Output "Directory Not Exception"
}
catch [System.IO.IOException]
{
Write-Output "Input/output Exception"
}
catch [System.IO.FileNotFoundException]
{
Write-Output "File Not Found Exception"
}

参数

设置错误操作参数可以通过多种方式完成。以下是 ErrorAction 参数的可能值。

  • If -ErrorAction Stop:抛出错误并停止程序的执行。
  • If -ErrorAction 查询/继续:抛出错误,但继续执行程序。
  • 如果-ErrorAction SilentlyContinue:不抛出错误,继续执行
  • If - ErrorAction Ignore:不抛出错误,继续执行

静默继续和忽略之间的区别在于静默将错误详细信息存储在 $error 变量中,而忽略则不会这样做。

PowerShell 中的 Try-catch 示例

下面是 Try、catch 块的示例用法:

代码:

try
{
dfdgfdfgf
sfsdf
dsfdfdsf
}
catch
{
write-host "Exception caught"
}

输出:

[玩转系统] PowerShell 中的 Try-catch |在 PowerShell 中使用 Try-catch 的指南

例子#1

ErrorActionPreference 的各种参数。

代码:

Write-Host "Example Error Action Preference: stop"
$ErrorActionPreference = 'Stop'
$test= 9/0
Write-Host "Still running"

输出:

[玩转系统] PowerShell 中的 Try-catch |在 PowerShell 中使用 Try-catch 的指南

[玩转系统] PowerShell 中的 Try-catch |在 PowerShell 中使用 Try-catch 的指南

正如您所看到的,发生错误后立即停止执行。

让我们更改它以继续

代码:

Write-Host " Demo"
$ErrorActionPreference = 'Continue'
$test= 9/0
Write-Host "Still running"

输出:

[玩转系统] PowerShell 中的 Try-catch |在 PowerShell 中使用 Try-catch 的指南

如果您在上面的输出中看到,即使发生错误后也会打印“Still running”

让我们看看 ErrorActionSet 设置为“静默继续”的相同输入

代码:

Write-Host " Sample test"
$ErrorActionPreference = 'SilentlyContinue'
$test= 9/0
Write-Host "Still running"

输出:

[玩转系统] PowerShell 中的 Try-catch |在 PowerShell 中使用 Try-catch 的指南

正如您所看到的,屏幕上没有显示任何错误,并且继续执行。

例子#2

将错误消息写入控制台。

代码:

Try {
Get-ThisWontWorktestcommand
}
Catch  {
Write-Host "The command ran is not found"`n -ForegroundColor Blue
Write-Host "Message: [$($_.Exception.Message)"] -ForegroundColor Red -BackgroundColor DarkBlue
}

输出:

[玩转系统] PowerShell 中的 Try-catch |在 PowerShell 中使用 Try-catch 的指南

错误处理最佳实践

  • 如果出现异常,请尝试找到解决方案来完全避免这种情况,而不是进行异常处理。
  • 检查代码的功能,以防经常出现相同的错误。
  • 尝试捕获尝试块将返回的特定类型的异常,如果不知道将要发生的异常类型,则创建一个通用的 catch 块。
  • 最好在输出文件中捕获异常消息以及时间戳和异常消息。
  • 建议不要创建空的 catch 块。

结论

因此,本文详细介绍了 PowerShell 中的 try 块功能。对于任何立志成为 PowerShell 优秀开发人员的人来说,对错误处理机制有深入的了解非常重要。对于自动化作业,如果没有在代码中实现错误处理机制,管理员了解问题是什么或问题何时发生将是一场噩梦。

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

取消回复欢迎 发表评论:

关灯