يتم إلقاء كومة استدعاء 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
}
من الناحية المثالية، أود التقاط الوسائط إلى الأرقام الوظيفية وأرقام الخط وما إلى ذلك (كما ترى في Get-pscallstack)
تحرير: لتوضيح، إنها أثر مكدس PowerShell الذي لا أريده.
أي أفكار كيفية تحقيق هذا؟
ديف
المحلول
الخطأ الأخير يجلس في:
$error[0]
الكثير من المعلومات الجيدة هناك لتكريمها بما في ذلك آثار المكدس الاستثناء. هذا هو برنامج نصي قليل نسبة (حل - 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