{"id":503,"date":"2008-07-20T23:00:19","date_gmt":"2008-07-21T07:00:19","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/2008\/07\/20\/fun-with-errors\/"},"modified":"2008-07-18T08:14:03","modified_gmt":"2008-07-18T16:14:03","slug":"fun-with-errors","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2008\/07\/20\/fun-with-errors\/","title":{"rendered":"Fun with Errors"},"content":{"rendered":"<p>In the online PowerShell class I&#8217;ve been doing this week we&#8217;ve been discussing error handling. I explained that the built-in $error variable is an array that stores recent PowerShell exceptions.&nbsp; Here&#8217;s a quick list of fun things you can do with an error object.<\/p>\n<p>Want to see how many exceptions have occurred:<\/p>\n<table border=\"1\">\n<tbody>\n<tr>\n<td bgcolor=\"blue\">\n<pre><font color=\"#ffffff\">PS C:\\&gt; $error.count<br \/>96<br \/><\/font><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Want to see the most recent exception:<\/p>\n<table height=\"67\" border=\"1\" width=\"500\" style=\"\">\n<tbody>\n<tr>\n<td bgcolor=\"#0000ff\" width=\"965\">\n<pre><font color=\"#ffffff\">PS C:\\&gt; $error[0]<br \/>Cannot convert value &quot;named&quot; to type &quot;System.Int32&quot;. Error: &quot;Input string was not in a correct format.&quot;At line:1 char:64<br \/>+ (($_.position -ne $()) -and ($_.position -ne &quot;&quot;) -and ([int]$_.p &lt;&lt;&lt;&lt; osition -ne $()))<\/font><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Did you know that there is more to this object?&nbsp; Pipe $error to Get-Member to see for yourself. Or pipe it to Select-object as I&#8217;ve done here:<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2008\/07\/error.png\"><img loading=\"lazy\" decoding=\"async\" height=\"471\" border=\"0\" width=\"644\" style=\"border-width: 0px;\" alt=\"error\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2008\/07\/error-thumb.png\" \/><\/a><\/p>\n<p>Many of the properties themselves are objects.<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2008\/07\/error2.png\"><img loading=\"lazy\" decoding=\"async\" height=\"471\" border=\"0\" width=\"644\" style=\"border-width: 0px;\" alt=\"error2\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2008\/07\/error2-thumb.png\" \/><\/a><\/p>\n<p>You can really drill down into the object to find out what went wrong.<\/p>\n<p>Finally, by default PowerShell only keeps track of the last 256 errors. Once $error has more than 256 entries, the older ones are removed to make room for newer errors. The $maximumErrorCount variable determines $error&#8217;s size which you can easily change.<\/p>\n<table border=\"1\">\n<tbody>\n<tr>\n<td bgcolor=\"blue\">\n<pre><font color=\"#ffffff\">PS C:\\&gt; $maximumErrorCount =512<br \/><\/font><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>So the next time an error occurs, don&#8217;t panic. Have some fun with it any maybe learn a little something as well.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the online PowerShell class I&#8217;ve been doing this week we&#8217;ve been discussing error handling. I explained that the built-in $error variable is an array that stores recent PowerShell exceptions.  Here&#8217;s a quick list of fun things you can do with an error object.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[25],"tags":[235,28,236,237],"class_list":["post-503","post","type-post","status-publish","format-standard","hentry","category-windows-powershell","tag-error","tag-powershell","tag-shell","tag-variables"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/503","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=503"}],"version-history":[{"count":2,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/503\/revisions"}],"predecessor-version":[{"id":504,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/503\/revisions\/504"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=503"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=503"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=503"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}