在PowerShell中捕获级联错误 [英] Catching Cascading Errors in PowerShell

查看:83
本文介绍了在PowerShell中捕获级联错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个包装脚本,该脚本调用一个函数来查询指定服务器内的某些数据库,并将元数据插入到单独服务器上的特定数据库中。我使用$ error.count变量来确定脚本是否成功。我预计会发生一些许可/提取错误,并希望这些错误被捕获和忽略($ error.count变量没有增加,但作业日志中写有警告)。我可以确认一个权限错误正在发生并且已被正确捕获。 $ error.count变量没有增加,但是从捕获显示了警告,显示了无法访问的数据库。

I'm working with a wrapper script that calls a function which queries some databases inside specified servers and inserts metadata to a specific database on a separate server. I use the $error.count variable to determine if the script was successful or not. I anticipate some permission/extraction errors to happen and want these to be caught and ignored (no increase in the $error.count variable but having a warning written in the job log). I can confirm one permission error is happening and is being properly caught. The $error.count variable is not increased but a warning is printed from the catch showing the database that could not be accessed.

提取/插入功能后出现了我的问题完成运行。在此函数返回到包装脚本后,我立即再次打印了$ error.count变量。这次,它返回1,就好像先前捕获的错误级联到包装脚本中一样。如前所述,我不希望将其包括在错误计数中。我不确定如何或为什么通过此函数增加$ error.count。

My problem occurs after the extraction/insertion function is finished running. Immediately after this function returns to the wrapper script, I have the $error.count variable print again. This time, it returns a 1 as if the error previously caught cascades into the wrapper script. As I mentioned previously, I do not want this to be included in the error count. I'm not sure how or why the $error.count is increased from this function.

我应该使用其他变量来确定脚本是否失败吗?是否有一些根本原因导致$ error.count在出现错误的函数之外增加而在捕获错误后却没有增加?

Should I use a different variable to determine if the script "failed" or not? Is there some underlying reason why the $error.count would increase outside of the function that has the error while not increasing after the error is caught? Any guidance on this issue would be appreciated.

参考代码:
包装函数:

Code for reference: Wrapper function:

$errorCount = $error.count
    Write-Warning ("$errorCount Before function")
    Extraction/Insertion_Function -serverList $serverList -insertionDB $insertionDB -ErrorAction SilentlyContinue
    $errorCount = $error.count
    Write-Warning ("$errorCount After function")
    } catch {
    Write-Error "Error caught by wrapper: $_"
    }

提取/插入功能:

ForEach ($db in $dbList) {
Write-Warning "$errorCount database
.
.
.
    try {
            $totalProperties = Get-ServerDBMetadata -DBConnectionString ($connStr) -DatabaseName $dbName -EA SilentlyContinue 
            } catch {
                Write-Warning "Unable to extract metadata from $dbname in $server"
                }
}

然后,我在循环中打印出错误计数,该循环从每个数据库中提取/插入元数据到插入数据库,以及在每个包含数据库的服务器的循环中:

I then have the error count printing out inside the loop that extracts/inserts the metadata from each database to the insertion database, as well as in the loop for each server that contains the databases:

WARNING: 0 Before function
WARNING: 0 database
.
.
.
WARNING: 0 database
WARNING: Unable to extract metadata from *database* in *server*
WARNING: 0 database
.
.
.
WARNING: 0 database
**WARNING: 1 After function**

错误(权限问题)被捕获在函数内部,但级联到我的包装器脚本。我希望忽略该特定错误,而不是忽略其他更严重的错误(例如无法连接到我要插入元数据的服务器),因此将-EA Ignore放在包装脚本内的驱动程序函数上问题。

The error (permission issue) is caught inside the function but cascades to my wrapper script. I want this particular error to be ignored while NOT ignoring other, more serious errors (like being unable to connect to the server I'm inserting the metadata into) so placing -EA Ignore on the driver function inside the wrapper script is out of the question.

推荐答案

您的主要问题是try-catch无法捕获错误(即使您没有提供所有代码)是您的cmdlet显式调用 -ErrorAction SilentlyContinue 。 Try / Catch块要求使用终止错误,因此对于函数/ cmdlet,您需要更改为 -ErrorAction Stop 才能使try / catch正确处理错误

Your primary problem with the try-catch not catching the error (even though you don't supply all of the code) is that your cmdlet explicitly calls -ErrorAction SilentlyContinue. Try/Catch blocks REQUIRE the use of terminating errors so in the case of your function/cmdlet, you need to change to -ErrorAction Stop for try/catch to appropriately handle an error from that function/cmdlet.

我们需要为代码中看不到的其他任何功能/ cmdlet进行更新。

This needs to be updated for any other function/cmdlet in the code we can't see.

编辑在以下注释中描述:

Edit described in comments below:

 $n = New-Object PSObject -property @{
    'Test1' = ''
    'Test2' = ''
    'Test3' = ''
}

try {
    get-process someprocess -ErrorAction Stop
    $n.Test1 = $true
} catch {
    $n.Test1 = $false
}

try {
    Get-WmiObject win32_computersystem -ErrorAction Stop
    $n.Test2 = $true
} catch {
    $n.Test2 = $false
}

try {
    Get-Content somefile.ext -ErrorAction Stop
    $n.Test3 = $true
} catch {
    $n.Test3 = $false
}


if ($n.Test1 -and $n.Test2 -and $n.Test3) {
    ## All procedures completed successfully -- do something magical
} else {
    ## At least one test procedure failed.
}

这篇关于在PowerShell中捕获级联错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆