PowerShell 技能连载 - 在 try/catch 中使用 ConvertFrom-ErrorRecord

在前一个技能中我们创建了一个名为 ConvertFrom-ErrorRecord 的函数,它能方便地从 PowerShell 的 ErrorRecord 对象中方便地获取错误信息。

您也可以在 catch 块中使用这个函数。只需要先运行以下函数即可:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
function ConvertFrom-ErrorRecord
{
[CmdletBinding(DefaultParameterSetName="ErrorRecord")]
param
(
[Management.Automation.ErrorRecord]
[Parameter(Mandatory,ValueFromPipeline,ParameterSetName="ErrorRecord", Position=0)]
$Record,

[Object]
[Parameter(Mandatory,ValueFromPipeline,ParameterSetName="Unknown", Position=0)]
$Alien
)

process
{
if ($PSCmdlet.ParameterSetName -eq 'ErrorRecord')
{
[PSCustomObject]@{
Exception = $Record.Exception.Message
Reason = $Record.CategoryInfo.Reason
Target = $Record.CategoryInfo.TargetName
Script = $Record.InvocationInfo.ScriptName
Line = $Record.InvocationInfo.ScriptLineNumber
Column = $Record.InvocationInfo.OffsetInLine
}
}
else
{
Write-Warning "$Alien"
}
}
}

以下是如何在 catch 代码块中使用 ConvertFrom-ErrorRecord 的例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
try
{
# this raises an error
Get-Service -Name NonExisting -ErrorAction Stop
}
catch
{
# pipe the errorrecord object through the new function
# to retrieve all relevant error information
# which you then could use to do error logging, or output
# custom error messages
$_ | ConvertFrom-ErrorRecord

}

本文国际来源:Using ConvertFrom-ErrorRecord in try/catch

评论