private/ResolveError.ps1
# borrowed from Jeffrey Snover http://blogs.msdn.com/powershell/archive/2006/12/07/resolve-error.aspx # modified to better handle SQL errors function ResolveError { [CmdletBinding()] param( [Parameter(ValueFromPipeline=$true)] $ErrorRecord=$Error[0], [Switch] $Short ) process { if ($_ -eq $null) { $_ = $ErrorRecord } $ex = $_.Exception if (-not $Short) { $error_message = "`nErrorRecord:{0}ErrorRecord.InvocationInfo:{1}Exception:`n{2}" $formatted_errorRecord = $_ | format-list * -force | out-string $formatted_invocationInfo = $_.InvocationInfo | format-list * -force | out-string $formatted_exception = '' $i = 0 while ($ex -ne $null) { $i++ $formatted_exception += ("$i" * 70) + "`n" + ($ex | format-list * -force | out-string) + "`n" $ex = $ex | SelectObjectWithDefault -Name 'InnerException' -Value $null } return $error_message -f $formatted_errorRecord, $formatted_invocationInfo, $formatted_exception } $lastException = @() while ($ex -ne $null) { $lastMessage = $ex | SelectObjectWithDefault -Name 'Message' -Value '' $lastException += ($lastMessage -replace "`n", '') if ($ex -is [Data.SqlClient.SqlException]) { $lastException += "(Line [$($ex.LineNumber)] " + "Procedure [$($ex.Procedure)] Class [$($ex.Class)] " + " Number [$($ex.Number)] State [$($ex.State)] )" } $ex = $ex | SelectObjectWithDefault -Name 'InnerException' -Value $null } $shortException = $lastException -join ' --> ' $header = $null $current = $_ $header = (($_.InvocationInfo | SelectObjectWithDefault -Name 'PositionMessage' -Value '') -replace "`n", ' '), ($_ | SelectObjectWithDefault -Name 'Message' -Value ''), ($_ | SelectObjectWithDefault -Name 'Exception' -Value '') | ? { -not [String]::IsNullOrEmpty($_) } | Select -First 1 $delimiter = '' if ((-not [String]::IsNullOrEmpty($header)) -and (-not [String]::IsNullOrEmpty($shortException))) { $delimiter = ' [<<==>>] ' } return "$($header)$($delimiter)Exception: $($shortException)" } } |