简体   繁体   English

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

[英]Capture line number of exception in dot sourced file

So I am running a powershell script within my script, whenever it fails the line number of the exception is the line number of where I call the dot sourced file in my parent script. 因此,只要脚本失败,我就会在脚本中运行Powershell脚本,例外的行号就是我在父脚本中调用点源文件的行号。

I tried to catch the exception, but that didn't contain the full exception, only the actual exception text (no line numbers or categories). 我试图捕获异常,但是其中不包含完整的异常,仅包含实际的异常文本(没有行号或类别)。

Has anyone solved this? 有人解决了吗? I've been searching high and low and haven't found anything about this. 我一直在搜寻高空搜寻,却未找到任何关于此的信息。

Error objects have a number of properties. 错误对象具有许多属性。 Output of $Error[0] | Get-Member | Select Name | Set-Clipboard $Error[0] | Get-Member | Select Name | Set-Clipboard输出$Error[0] | Get-Member | Select Name | Set-Clipboard $Error[0] | Get-Member | Select Name | Set-Clipboard $Error[0] | Get-Member | Select Name | Set-Clipboard : $Error[0] | Get-Member | Select Name | Set-Clipboard

Name 名称

Equals 等于
GetHashCode GetHashCode的
GetObjectData GetObjectData使用
GetType 的GetType
ToString 的ToString
CategoryInfo CategoryInfo
ErrorDetails 错误详情
Exception 例外
FullyQualifiedErrorId InvocationInfo FullyQualifiedErrorId InvocationInfo
PipelineIterationInfo ScriptStackTrace PipelineIterationInfo ScriptStackTrace
TargetObject TargetObject
PSMessageDetails PSMessageDetails

So, you could throw the details to the console via a function, for example: 因此,您可以通过函数将详细信息扔到控制台,例如:

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
}

In your script, if you have a try/catch block, you could catch exceptions and call your function: 在脚本中,如果有一个try / catch块,则可以捕获异常并调用函数:

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{}

If saved inside of a file called 123.ps1 and run, your output would look like this: 如果保存在名为123.ps1的文件中并运行,您的输出将如下所示:

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

The ScriptStackTrace property might be helpful for troubleshooting, especially if you are writing scripts/tools for an audience and not just your own use. ScriptStackTrace属性可能有助于解决问题,特别是如果您要为观众编写脚本/工具而不仅仅是自己使用。 You could add additional functions to do logging with the objects Write-ErrorDetails can provide, etc etc 您可以添加其他功能来使用Write-ErrorDetails可以提供的对象进行记录等

Hope that helps! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM