Most errors which occur in your working scripts are likely to be "non-terminating". This means that Monad just reports the error and the command keeps running. ("Terminating" errors such as syntax errors will halt the command and, in some cases, the entire script; see http://blogs.msdn.com/monad/archive/2005/11/15/493102.aspx for more details.).
This Blog describes how to handle non-terminating errors. You may choose to modify the action that occurs when a non-terminating error occurs or you may choose to collect non-terminating errors in a variable. If you collect non-terminating errors in a variable, your script can check to see if any the prior command emitted any non-terminating errors.
Consider:
MSH C:\temp\monad> remove-item nosuchfile.txt;write-host "done"remove-item : Cannot find path 'C:\temp\monad\nosuchfile.txt' because it does not exist.At line:1 char:12+ remove-item <<<< nosuchfile.txt;write-host "done"doneMSH C:\temp\monad>
Even though the file was not found, the script just kept on running. This is often the case with scripts which are syntactically correct but encounter operational errors. So, what can you do to cause a script to react to problems it might encounter?
(1) -ErrorAction
You can change how Monad reacts to non-terminating errors with the "-ErrorAction" ubiquitous parameter ("-ea" is the alias). Your options are SilentlyContinue, Continue, Stop, and Inquire (also see http://blogs.msdn.com/monad/archive/2005/11/11/491967.aspx):
You can suppress printing errors for a specific cmdlet with "-ea SilentlyContinue", or in general with "$ErrorActionPreference = "SilentlyContinue"". You can cause non-terminating errors to become terminating (the type will be ActionPreferenceStopException) with "-ea Stop" or "$ErrorActionPreference = "Stop"".
MSH C:\temp\monad> remove-item nosuchfile.txt -ea Stopremove-item : Command execution stopped because the shell variable "ErrorActionPreference" is set to Stop: Cannot find path 'C:\temp\monad\nosuchfile.txt' because it does not exist.At line:1 char:12+ remove-item <<<< nosuchfile.txt -ea StopMSH C:\temp\monad> remove-item nosuchfile.txt -ea SilentlyContinueMSH C:\temp\monad>
(2) -ErrorVariable
You can save off the non-terminating errors to a shell variable using the "-ErrorVariable" ubiquitous parameter ("-ev" is the parameter alias). This is not affected by "-ErrorAction".
MSH C:\temp\monad> get-content errorvariable.mshremove-item foo.txt -ErrorVariable errs -ErrorAction SilentlyContinueif ($errs.Count -eq 0){ write-host "ran fine"}else{ write-host "failed"}MSH C:\temp\monad> .\errorvariable.mshfailedMSH C:\temp\monad>
You can also append failures to an existing ErrorVariable by prepending "+" to the variable name. This even works for multiple cmdlets in the same pipeline appending to the same ErrorVariable.
MSH C:\temp\monad> gc errorvariable.msh$ErrorActionPreference = "SilentlyContinue"
remove-item nosuchfile.txt -ErrorVariable errsremove-item nosuchfile2.txt -ErrorVariable +errs$errs.CountMSH C:\temp\monad> .\errorvariable.msh2