エラーがスローされた後のPowerShellキャプチャコールスタック
-
19-09-2019 - |
質問
私はこのような何かをしたい...
try
{
# Something in this function throws an exception
Backup-Server ...
}catch
{
# Capture stack trace of where the error was thrown from
Log-Error $error
}
理想的には私は
(あなたが得る-pscallstackに見られるような)関数と行番号などの引数をキャプチャしたいのですが
編集:私は、.NET 1
ない欲しい、それはPowerShellのスタックトレースですが、明確にするために、
これを達成するためにどのように任意のアイデア?
デーブ
解決
最後のエラーがで座ってます:
$error[0]
あなたは例外スタックトレースを含む追いかけるためにはそこに良い情報がたくさん。これはhandly小さなスクリプトです(解決-ErrorRecordそのPSCXに同梱)最後のエラーについての良い情報の多くを示しています:
param(
[Parameter(Position=0, ValueFromPipeline=$true)]
[ValidateNotNull()]
[System.Management.Automation.ErrorRecord[]]
$ErrorRecord
)
process {
if (!$ErrorRecord)
{
if ($global:Error.Count -eq 0)
{
Write-Host "The `$Error collection is empty."
return
}
else
{
$ErrorRecord = @($global:Error[0])
}
}
foreach ($record in $ErrorRecord)
{
$record | Format-List * -Force
$record.InvocationInfo | Format-List *
$Exception = $record.Exception
for ($i = 0; $Exception; $i++, ($Exception = $Exception.InnerException))
{
"$i" * 80
$Exception | Format-List * -Force
}
}
}
他のヒント
あなたはキースの答えと同じくらいのコードを必要としません。
$error[0].ErrorRecord.ScriptStackTrace
あなたが望むものである。
所属していません StackOverflow