Powershell-Capture-Call-Stack, nachdem ein Fehler ausgelöst wird,
-
19-09-2019 - |
Frage
Ich möchte etwas tun ...
try
{
# Something in this function throws an exception
Backup-Server ...
}catch
{
# Capture stack trace of where the error was thrown from
Log-Error $error
}
Im Idealfall würde Ich mag Argumente erfassen, um die Funktion und Zeilennummern usw. (wie Sie in get-PsCallStack sehen)
EDIT: Um zu klären, ist es der Powershell-Stack-Trace Ich mag nicht das .NET ein
Irgendwelche Ideen, wie dies zu erreichen?
Dave
Lösung
Der letzte Fehler sitzt in:
$error[0]
Es gibt viele gute Informationen dort für Sie mit Ausnahme Stack-Traces zu jagen. Dies ist ein handliches kleines Skript (Resolve-Errorrecord, dass Schiffe mit PSCX), die vielen guten Informationen über den letzten Fehler zeigt:
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
}
}
}
Andere Tipps
Sie haben nicht so viel Code wie Keith Antwort benötigen.
$error[0].ErrorRecord.ScriptStackTrace
ist das, was Sie wollen.
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow