2

So I've been doing my error handling in Powershell with Try/Catch so far and whenever an error occurs, it gets written to a log file.

Now how could I handle unexpected errors? Should I just put the whole script code in a Try/Catch block or is there a better method to do this?

Thanks for any help.

4
  • I think Try/Catch is the best approach to handle errors in Powershell. What is the purpose of handling any other "unexpeced" error? Isn't better handling specifically any part of code that actually could generate errors? Commented Apr 21, 2015 at 8:31
  • Yes, of course and I do that with the major part of any script I make. But putting every Get-Date or Set-Variable in a Try/Catch block makes the script unnecessary long imo. Additionally, those are commands that I've never seen failing to execute so I focus on rather instable parts of my scripts and use Try/Catch there. Every other error can be an unexpected one since I literally do not expect my scripts to crash there. Commented Apr 21, 2015 at 9:02
  • I agree with you, that would be unnecessary. So why catching unlikely errors on Get-Date or Set-Variable? How can you usefully handle them? Commented Apr 21, 2015 at 9:37
  • 1
    I have a function called ErrorCollector in every script I make. The error then gets logged and is also stored in a variable with every other error that occured so far. At the end of every script a mail gets sent to my helpdesk IF at least one error occured or a notification has been set. That's why I also want to log unexpected errors. I just want to get notified of any error that occurs. Commented Apr 21, 2015 at 11:08

2 Answers 2

2

Yes, there is. You can define a Trap at the top of your script and Log the last error:

trap
{
    Write-host $Error[0] 
}
Sign up to request clarification or add additional context in comments.

1 Comment

That's a good hint but when I use trap, PowerShell displays the error in the console and I don't want that. I can't set my $ErrorActionPreference to SilentlyContinue because then my Try/Catch won't work. Is there a way to use Trap without ouput?
1

You are right. When you use the default try/catch(/finally) statements all exception will be trapped in the catch block.

try { 

  Do-Someting

} catch {

  Write-Host "Caught the following error: $($_.Exception.Message)"

} finally {

  Write-Host "Finally, we made it!"

}

When you specifically add an exception to catch, you can create specific actions for that exception:

try{

  Do-Something

} catch [System.Management.Automation.ItemNotFoundException]{

  # catching specific exceptions allows you to have
  # custom actions for different types of errors
  Write-Host "Caught an ItemNotFoundException: $($_.Exception.Message)" -ForegroundColor Red

} catch {

  Write-Host "General exception: $($_.Exception.Message)"

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.