{"id":10671,"date":"2006-04-25T12:18:02","date_gmt":"2006-04-25T12:18:02","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2006\/04\/25\/managing-non-terminating-errors\/"},"modified":"2019-02-18T13:24:58","modified_gmt":"2019-02-18T20:24:58","slug":"managing-non-terminating-errors","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/managing-non-terminating-errors\/","title":{"rendered":"Managing non-terminating errors"},"content":{"rendered":"<p>Most errors which occur in your working scripts are likely to be &#8220;non-terminating&#8221;.&nbsp; This means that Monad just reports the error and the command keeps running.&nbsp; (&#8220;Terminating&#8221; errors such as syntax errors will halt the command and, in some cases, the entire script; see <a href=\"http:\/\/blogs.msdn.com\/monad\/archive\/2005\/11\/15\/493102.aspx\">http:\/\/blogs.msdn.com\/monad\/archive\/2005\/11\/15\/493102.aspx<\/a> for more details.).&nbsp;<\/p>\n<p>This Blog describes how to handle non-terminating errors.&nbsp; 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.&nbsp; 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.<\/p>\n<p>Consider:<\/p>\n<p><font face=\"Courier New\">MSH C:\\temp\\monad&gt; remove-item nosuchfile.txt;write-host &#8220;done&#8221;<br \/>remove-item : Cannot find path &#8216;C:\\temp\\monad\\nosuchfile.txt&#8217; because it does not exist.<br \/>At line:1 char:12<br \/>+ remove-item&nbsp; &lt;&lt;&lt;&lt; nosuchfile.txt;write-host &#8220;done&#8221;<br \/>done<br \/>MSH C:\\temp\\monad&gt;<\/font>&nbsp;<\/p>\n<p>Even though the file was not found, the script just kept on running.&nbsp; This is often the case with scripts which are syntactically correct but encounter operational errors.&nbsp; So, what can you do to cause a script to react to problems it might encounter?<\/p>\n<p>(1) -ErrorAction&nbsp;<\/p>\n<p>You can change how Monad reacts to non-terminating errors with the &#8220;-ErrorAction&#8221; ubiquitous parameter (&#8220;-ea&#8221; is the alias).&nbsp; Your options are SilentlyContinue, Continue, Stop, and Inquire (also see <a href=\"http:\/\/blogs.msdn.com\/monad\/archive\/2005\/11\/11\/491967.aspx\">http:\/\/blogs.msdn.com\/monad\/archive\/2005\/11\/11\/491967.aspx<\/a>):<\/p>\n<ul>\n<li>&#8220;SilentlyContinue&#8221;: Do not print, continue<\/li>\n<li>&#8220;Continue&#8221;: Print, continue (this is the default)<\/li>\n<li>&#8220;Stop&#8221;: Halt the command or script<\/li>\n<li>&#8220;Inquire&#8221;: Ask the user what to do <\/li>\n<\/ul>\n<p>You can suppress printing errors for a specific cmdlet with &#8220;-ea SilentlyContinue&#8221;, or in general with &#8220;$ErrorActionPreference = &#8220;SilentlyContinue&#8221;&#8221;.&nbsp; You can cause non-terminating errors to become terminating (the type will be ActionPreferenceStopException) with &#8220;-ea Stop&#8221; or &#8220;$ErrorActionPreference = &#8220;Stop&#8221;&#8221;.<\/p>\n<p><font face=\"Courier New\">MSH C:\\temp\\monad&gt; remove-item nosuchfile.txt -ea Stop<br \/>remove-item : Command execution stopped because the shell variable &#8220;ErrorActionPreference&#8221; is set to Stop: Cannot find path &#8216;C:\\temp\\monad\\nosuchfile.txt&#8217; because it does not exist.<br \/>At line:1 char:12<br \/>+ remove-item&nbsp; &lt;&lt;&lt;&lt; nosuchfile.txt -ea Stop<br \/>MSH C:\\temp\\monad&gt; remove-item nosuchfile.txt -ea SilentlyContinue<br \/>MSH C:\\temp\\monad&gt;<br \/><\/font><\/p>\n<p>(2)&nbsp; -ErrorVariable<\/p>\n<p>You can save off the non-terminating errors to a shell variable using the &#8220;-ErrorVariable&#8221; ubiquitous parameter (&#8220;-ev&#8221; is the parameter alias).&nbsp; This is not affected by &#8220;-ErrorAction&#8221;.<\/p>\n<p><font face=\"Courier New\">MSH C:\\temp\\monad&gt; get-content errorvariable.msh<br \/>remove-item foo.txt -ErrorVariable errs -ErrorAction SilentlyContinue<br \/>if ($errs.Count -eq 0)<br \/>{<br \/>&nbsp;&nbsp;&nbsp; write-host &#8220;ran fine&#8221;<br \/>}<br \/>else<br \/>{<br \/>&nbsp;&nbsp;&nbsp; write-host &#8220;failed&#8221;<br \/>}<br \/>MSH C:\\temp\\monad&gt; .\\errorvariable.msh<br \/>failed<br \/>MSH C:\\temp\\monad&gt;<\/font><\/p>\n<p>You can also append failures to an existing ErrorVariable by prepending &#8220;+&#8221; to the variable name.&nbsp; This even works for multiple cmdlets in the same pipeline appending to the same ErrorVariable.<\/p>\n<p><font face=\"Courier New\">MSH C:\\temp\\monad&gt; gc errorvariable.msh<br \/>$ErrorActionPreference = &#8220;SilentlyContinue&#8221;<\/font><\/p>\n<p><font face=\"Courier New\">remove-item nosuchfile.txt -ErrorVariable errs<br \/>remove-item nosuchfile2.txt -ErrorVariable +errs<br \/>$errs.Count<br \/>MSH C:\\temp\\monad&gt; .\\errorvariable.msh<br \/>2<\/font><\/p>\n<p>&nbsp;<\/p>\n<p>[<i>Edit: Monad has now been renamed to Windows PowerShell.  This script or discussion may require slight adjustments before it applies directly to newer builds.<\/i>]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Most errors which occur in your working scripts are likely to be &#8220;non-terminating&#8221;.&nbsp; This means that Monad just reports the error and the command keeps running.&nbsp; (&#8220;Terminating&#8221; 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.).&nbsp; This Blog describes how to handle non-terminating [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-10671","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"blog_post_summary":"<p>Most errors which occur in your working scripts are likely to be &#8220;non-terminating&#8221;.&nbsp; This means that Monad just reports the error and the command keeps running.&nbsp; (&#8220;Terminating&#8221; 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.).&nbsp; This Blog describes how to handle non-terminating [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/10671","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=10671"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/10671\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=10671"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=10671"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=10671"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}