scobi
scobi

Reputation: 14558

Can PowerShell autoconvert error output into exceptions?

In my PowerShell script I'm calling some command line tools (not cmdlets) that may output error text and set errorlevel nonzero on error conditions. I'd like any error from any of these tools to abort my script.

What's the best way to do this?

Is there a way to have PowerShell automatically check $? and stderr after every command and throw an exception? I was thinking something along the lines of "on error" that 4NT or VBScript has - a global watch. Not very object-orienty, so I was hoping PowerShell had something better.

[Edit: last couple questions moved to here.]

Upvotes: 2

Views: 288

Answers (1)

ChrisLively
ChrisLively

Reputation: 88092

You will need to either write a function to do this or within the script constantly check for the errorlevel.

Some information on a function to do this is at:

http://www.eggheadcafe.com/conversation.aspx?messageid=31829234&threadid=31829225

and other useful information can be found on the windows powershell blog.

Upvotes: 1

Related Questions