Reputation: 664
I have a very large PowerShell script that when I use $error at the end of the script, I am getting a returned error, even though the Transcript is not showing one. So I have no clue where the "error" is at. Here is my setup:
$error.Clear()
# 600 lines of code
foreach ($err_msg IN $error) {
$body += "<br><hr>"
$body += "$err_msg"
}
It then sends that in an email to me. The issue I am having is it returns an error somewhere in the code that is not visible in Start-Transcript but I want to clean it up so its not reporting this error to me every night. If I just run $error in the console, I get line numbers for errors, but if I use the above code to log the errors, I do not get line numbers. Any way to display the full error with line numbers without using try/catch?
Upvotes: 0
Views: 1775
Reputation: 2342
You could format the errors using Select-Object
and then convert it to an html table using ConvertTo-Html -Fragment
if ( $error )
{
$ErrorTable = $Error | Select-Object -Property @(
@{Name = 'ScriptLineNumber'; Expression = { $_.InvocationInfo.ScriptLineNumber } }
@{Name = 'Line' ; Expression = { $_.InvocationInfo.Line } }
@{Name = 'Exception' ; Expression = { $_.Exception } }
) | ConvertTo-Html -Fragment
$Body += "<br><hr>"
$Body += $ErrorTable
}
Upvotes: 4