Solved

powershell 3+ try\catch error message output???

Posted on 2013-12-30
5
745 Views
Last Modified: 2013-12-31
This code works, but no matter what I try I cannot get $error or $erroroutput to show the error messages. I can type them manually in the console\output sections of the PS IDE when the program stops to see what the errors are.
Any ideas?
Try
{
$CountCSV = Invoke-Sqlcmd -Query $Query -Username "test" -Password "test" -ErrorAction Stop -QueryTimeout 65535  
}
catch{
    $errorout += $error[0]
#This next line outputs the text in the quotes, but I cannot get it to do anything else
    Write-Error "CountCSV major error, query failed problem"
#this next line only shows the text in the quotes:
    Write-Error "CountCSV major error, query failed problem" $errorout
#nothing here shows up at all
            write-host $errorout
$error
$errorout
}

Again, I can type $error to see an error message and $errorout to see another error message. One is generic and the other is detailed. For example if there's a query time-out, one will basically say there's a networking issue while the other actual tells the query exceeded the timeout period. I would like to shows these 2 outputs automatically instead of having to type them to see what went wrong.
From what I've read on the Internet it should be working as expected, but it is acting like $error and $errorout are empty in the above example.

I've also used the same Try\Catch code in several invoke-sql sections of the program and none of them display the desired information.
0
Comment
Question by:johnj_01201
  • 2
  • 2
5 Comments
 
LVL 39

Assisted Solution

by:footech
footech earned 250 total points
ID: 39747086
Write-Error "CountCSV major error, query failed problem" $errorout
won't work.  In fact that should generate an error.  You can only give it one message to display.
In testing it appears your problem (that $error and $errorout don't output anything in the catch block) is caused by this issue.  Can't say for certain, but maybe it's because PS doesn't handle errors within a catch block well.  That's pure speculation on my part, since I don't know how PS handles errors within a catch block, but it seems to fit the behavior.

Is $errorout defined earlier as an empty array (e.g. $errorout = @() )?
If not then there could be errors when trying to add to it with +=.

Write-Host really only works to output a string, not the entire object, so be conscious of what you output with it.  If you try to output an object with it, it will try to convert it to a string, so that could explain the difference you're seeing between the error messages.
To start off with error handling, I probably wouldn't recommend using Write-Error to begin with.
0
 
LVL 68

Accepted Solution

by:
Qlemo earned 250 total points
ID: 39747824
I've used Write-Error in http://www.experts-exchange.com/Q_28325801.html just for demonstration purposes for that specific test case - because you told the recursion error does not appear, which it does. It generates a new error, and if errors in a catch block occur execution is halted. (You can, of course, nest try/catch to achieve error handling in error handling.)

Keep in mind $error contains all errors (exceptions) which were triggered in the PS session's lifetime, unless $error.Clear() is called. Dumping that var will always supply more info than using only the most recent entry, $error[0] (which is, btw, the same $_ in the catch block).

Also be aware that an object of $error is of type ErrorRecord and contains much more than only an error message. That should be used for proper handling and reporting. And that again requires that you think carefully about what should happen in each particular case.
The common methods are to collect error information in a var (as you did with $errorout), and dump that later.
Or check the  error category, and stop the script, e.g. with the Write-Error cmdlet.
0
 
LVL 1

Author Closing Comment

by:johnj_01201
ID: 39748177
Thanks. I guess the try\catch is not as simple as I thought. I will research and learn more about it.
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 39748547
Proper error handling is indeed a more advanced topic ...
0
 
LVL 39

Expert Comment

by:footech
ID: 39749073
I can't remember where I've read it, but I believe I've seen it said a few times that error handling can easily double the size of your script, and take twice as long to write as the main script portion (or more).
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article will help you understand what HashTables are and how to use them in PowerShell.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now