{"id":10901,"date":"2006-04-25T12:17:51","date_gmt":"2006-04-25T12:17:51","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2006\/04\/25\/debugging-monad-scripts-part-2-error\/"},"modified":"2019-02-18T13:25:06","modified_gmt":"2019-02-18T20:25:06","slug":"debugging-monad-scripts-part-2-error","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/debugging-monad-scripts-part-2-error\/","title":{"rendered":"Debugging Monad Scripts, Part 2: $error"},"content":{"rendered":"<p>\t\t<font size=\"2\"><font size=\"3\"><\/p>\n<p>Did your command or script fail and\/or report an error?&nbsp; We hope to have a proper script debugger in a future version, but until then, MSH has some handy features to help you figure out what went wrong.&nbsp; In this series of blog entries, I will present some of those features.&nbsp; Thanks to Jim Truher [MSFT], Bruce Payette [MSFT], and Jeff Jones [MSFT] for their help in putting this together.<\/p>\n<p>See the Windows &#8220;Monad&#8221; Shell Beta 2 Documentation Pack (<a href=\"http:\/\/www.microsoft.com\/downloads\/details.aspx?FamilyID=8a3c71d1-18e5-49d7-952a-c55d694ecee3&amp;displaylang=en\"><font color=\"#0000ff\">http:\/\/www.microsoft.com\/downloads\/details.aspx?FamilyID=8a3c71d1-18e5-49d7-952a-c55d694ecee3&amp;displaylang=en<\/font><\/a>) for general information about Monad.<\/p>\n<p>Part 1: <a href=\"http:\/\/blogs.msdn.com\/monad\/archive\/2005\/11\/04\/489138.aspx\">http:\/\/blogs.msdn.com\/monad\/archive\/2005\/11\/04\/489138.aspx<\/a>&nbsp;(Terminating vs. Non-Terminating Errors, ErrorRecord)<\/p>\n<p>Jon Newman [MSFT]<\/p>\n<p>&nbsp;<\/p>\n<p><\/font><\/font><b>$error<\/p>\n<p><\/b><font color=\"#000080\"><\/font><font size=\"2\"><\/p>\n<p><font size=\"3\">Errors occurring in MSH commands or scripts will be stored in $error. $error is always on, so you can use it even for errors which you can&#8217;t reproduce (or would rather not repeat).<\/font><\/p>\n<p><font size=\"3\">$error is an ArrayList containing the most recent errors: $error[0] is the most recent error, $error[1] the one before that, etc. $MaximumErrorCount (default 256) is the maximum number of errors which will be stored before the oldest is discarded.<\/font><\/p>\n<p><font size=\"3\">So, when you see errors, you should have a look at $error[0]. Note that any new error will move $error[0] to $error[1], so you&#8217;ll probably want to copy $error[0] to another shell variable before investigating it, e.g.<\/font><\/p>\n<p><\/font><font size=\"2\"><\/p>\n<p><font face=\"Courier New\">MSH C:\\temp&gt; start-service $servicename<br \/>start-service : Cannot bind argument to parameter &#8216;ServiceName&#8217; because it is null.<br \/>At line:1 char:14<br \/>+ start-service&nbsp; &lt;&lt;&lt;&lt; $servicename<br \/>MSH C:\\temp&gt; $savethis = $error[0]<br \/>MSH C:\\temp&gt; $savethis.GetType().Name<br \/>ErrorRecord<br \/>MSH C:\\temp&gt; $savethis.InvocationInfo.MyCommand<\/font><\/p>\n<p><font face=\"Courier New\">CommandType&nbsp;&nbsp;&nbsp;&nbsp; Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Definition<br \/>&#8212;&#8212;&#8212;&#8211;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;-<br \/>Cmdlet&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; start-service&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; start-service [-ServiceName] String[] [-PassThru&#8230;<\/font><\/p>\n<p><font face=\"Courier New\"><\/p>\n<p>MSH C:\\temp&gt;<\/font><\/font><\/p>\n<p><font size=\"2\"><font size=\"3\">Note that Exception objects will in some cases also appear in $error. In particular, the &#8220;throw&#8221; script command may add both an ErrorRecord and a RuntimeException to $error.<\/font><\/p>\n<p><\/font><\/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>Did your command or script fail and\/or report an error?&nbsp; We hope to have a proper script debugger in a future version, but until then, MSH has some handy features to help you figure out what went wrong.&nbsp; In this series of blog entries, I will present some of those features.&nbsp; Thanks to Jim Truher [&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-10901","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"blog_post_summary":"<p>Did your command or script fail and\/or report an error?&nbsp; We hope to have a proper script debugger in a future version, but until then, MSH has some handy features to help you figure out what went wrong.&nbsp; In this series of blog entries, I will present some of those features.&nbsp; Thanks to Jim Truher [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/10901","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=10901"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/10901\/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=10901"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=10901"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=10901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}