如何在PowerShell中获取错误的行号 [英] How to get the line number of error in PowerShell
本文介绍了如何在PowerShell中获取错误的行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图在运行 PowerShell 脚本时获取错误的行号.这是我目前正在使用的:
I'm trying to get the line number of an error when running a PowerShell script. Here is what I'm using at the moment:
$e = $_.Exception
$line = $_.Exception.InvocationInfo.ScriptLineNumber
$msg = $e.Message
Write-Host -ForegroundColor Red "caught exception: $e at $line"
有时这行得通,有时却行不通.我想知道我是否做错了什么,或者我可以做些什么来使这项工作更加一致.
Sometimes this works and sometimes it doesn't. I'm wondering if I'm doing anything wrong, or what I can do to make this work more consistently.
推荐答案
我想出了问题所在:
代替:
$e = $_.Exception
#this is wrong
$line = $_.Exception.InvocationInfo.ScriptLineNumber
$msg = $e.Message
Write-Host -ForegroundColor Red "caught exception: $e at $line"
应该是
$e = $_.Exception
$line = $_.InvocationInfo.ScriptLineNumber
$msg = $e.Message
Write-Host -ForegroundColor Red "caught exception: $e at $line"
这篇关于如何在PowerShell中获取错误的行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文