PowerShell Capture 통화 스택이 오류가 발생한 후
-
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
}
이상적으로는 함수 및 줄 번호 등에 대한 인수를 캡처하고 싶습니다 (Get-Pscallstack에서 볼 수 있듯이)
편집 : 명확히하기 위해, 그것은 PowerShell 스택 추적입니다.
이것을 달성하는 방법이 있습니까?
데이브
해결책
마지막 오류는 다음과 같습니다.
$error[0]
예외 스택 추적을 포함하여 추격 할 수있는 좋은 정보가 많이 있습니다. 이것은 마지막 오류에 대한 좋은 정보를 많이 보여주는 직접적인 스크립트 (PSCX와 함께 제공되는 Resolve-ErrorRecord)입니다.
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
}
}
}
다른 팁
Keith의 답변만큼 많은 코드가 필요하지 않습니다.
$error[0].ErrorRecord.ScriptStackTrace
당신이 원하는 것입니다.
제휴하지 않습니다 StackOverflow