捕获点源文件中的异常行号

时间:2018-06-08 13:44:57

标签: powershell

所以我在我的脚本中运行一个powershell脚本,每当它失败时,异常的行号是我在父脚本中调用点源文件的行号。

我试图捕获异常,但是它不包含完整的异常,只包含实际的异常文本(没有行号或类别)。

有人解决了这个问题吗?我一直在寻找高低,并没有找到任何关于这一点。

1 个答案:

答案 0 :(得分:2)

错误对象具有许多属性。 输出# GET TOP LEVEL SUBFOLDERS subfolders <- list.dirs(path="/my/root/path", recursive=FALSE) # ITERATE THROUGH ALL FILES OF EACH FOLDER master_list <- lapply(subfolders, function(f) { tmp_files <- list.files(path=f, pattern=".ext", full.names=TRUE) sapply(tmp_files, someFunction) # RETURNS MATRIX }) final_obj <- do.call(rbind, master_list) # MASTER MATRIX

名称

等于
GetHashCode的
GetObjectData使用
的GetType
的ToString
CategoryInfo
ErrorDetails
异常
FullyQualifiedErrorId InvocationInfo
PipelineIterationInfo ScriptStackTrace
TargetObject
PSMessageDetails

因此,您可以通过函数将详细信息抛给控制台,例如:

$Error[0] | Get-Member | Select Name | Set-Clipboard

在你的脚本中,如果你有一个try / catch块,你可以捕获异常并调用你的函数:

Function Write-ErrorDetails($ErrorObject)
{
    $thisError = [PSCustomObject]@{

        Exception = $ErrorObject.Exception
        Message = $ErrorObject.Exception.Message
        FQID = $ErrorObject.FullyQualifiedErrorId
        InovcationInfo = $ErrorObject.InvocationInfo
        ScriptStackTrace = $ErrorObject.ScriptStackTrace
        TargetObject = $ErrorObject.TargetObject
    }

    return $thisError
}

如果保存在名为123.ps1的文件中并运行,则输出将如下所示:

BEGIN
{
    Function Write-ErrorDetails($ErrorObject)
    {
        $thisError = [PSCustomObject]@{

            Exception = $ErrorObject.Exception
            Message = $ErrorObject.Exception.Message
            FQID = $ErrorObject.FullyQualifiedErrorId
            InovcationInfo = $ErrorObject.InvocationInfo
            ScriptStackTrace = $ErrorObject.ScriptStackTrace
            TargetObject = $ErrorObject.TargetObject
        }

        return $thisError
    }
}
PROCESS
{
    try
    {
        Do-SomeNonExistentCommand
    }
    catch
    {
        Write-ErrorDetails -ErrorObject $Error[0]
    }
}
END{}

Exception : System.Management.Automation.CommandNotFoundException: The term 'Do-SomeNonExistentCommand' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference( FunctionContext funcContext, Exception exception) at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(Int erpretedFrame frame) at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction .Run(InterpretedFrame frame) at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction .Run(InterpretedFrame frame) Message : The term 'Do-SomeNonExistentCommand' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. FQID : CommandNotFoundException InovcationInfo : System.Management.Automation.InvocationInfo ScriptStackTrace : at <ScriptBlock><Process>, C:\Users\Pythagoras\desktop\123.ps1: line 22 at <ScriptBlock>, <No file>: line 1 TargetObject : Do-SomeNonExistentCommand 属性可能有助于排除故障,尤其是在为受众编写脚本/工具而不仅仅是您自己的用途时。您可以添加其他功能来使用ScriptStackTrace可以提供的对象等进行日志记录

希望有所帮助!