{"id":55,"date":"2007-01-23T04:00:00","date_gmt":"2007-01-23T12:00:00","guid":{"rendered":"http:\/\/testblog.sapien.com\/index.php\/2007\/01\/23\/using-errorvariable\/"},"modified":"2007-01-23T04:00:00","modified_gmt":"2007-01-23T12:00:00","slug":"using-errorvariable","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2007\/01\/23\/using-errorvariable\/","title":{"rendered":"Using ErrorVariable"},"content":{"rendered":"<p>Another common parameter you might want to include in your PowerShell code is -ErrorVariable. Like -OutVariable that I <a href=\"http:\/\/sapien.eponym.com\/blog\/_archives\/2007\/1\/22\/2672998.html\">talked about yesterday<\/a>, you can redirect error information to a specified variable:<\/p>\n<\/p>\n<div class=\"wlWriterSmartContent\" id=\"57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:5c407cbd-6d9c-4c52-98b1-6904e6769acf\" contenteditable=\"false\" style=\"margin: 0px; padding: 0px; display: inline; float: none;\">\n<pre style=\"background-color: White;\"><div><!--Code highlighting produced by Actipro CodeHighlighter (freeware)http:\/\/www.CodeHighlighter.com\/--><span style=\"color: rgb(0, 0, 0);\">gwmi win32_operatingsystem <\/span><span style=\"color: rgb(0, 0, 0);\">-<\/span><span style=\"color: rgb(0, 0, 0);\">errorvariable oops <\/span><span style=\"color: rgb(0, 0, 0);\">|<\/span><span style=\"color: rgb(0, 0, 255);\">out<\/span><span style=\"color: rgb(0, 0, 0);\">-<\/span><span style=\"color: rgb(0, 0, 255);\">null<\/span><\/div><\/pre>\n<\/div>\n<p>In this situation, $oops will have no value since no error was returned. But what about this where $computer is a remote computer that you&#8217;ve mistyped, don&#8217;t have permission for or is down:<\/p>\n<\/p>\n<div class=\"wlWriterSmartContent\" id=\"57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:6af65b9d-dc5c-4579-ade4-e2163b8909f8\" contenteditable=\"false\" style=\"margin: 0px; padding: 0px; display: inline; float: none;\">\n<pre style=\"background-color: White;\"><div><!--Code highlighting produced by Actipro CodeHighlighter (freeware)http:\/\/www.CodeHighlighter.com\/--><span style=\"color: rgb(0, 0, 0);\">gwmi win32_operatingsystem <\/span><span style=\"color: rgb(0, 0, 0);\">-<\/span><span style=\"color: rgb(0, 0, 0);\">computer $computer <\/span><span style=\"color: rgb(0, 0, 0);\">-<\/span><span style=\"color: rgb(0, 0, 0);\">errorvariable oops <\/span><span style=\"color: rgb(0, 0, 0);\">|<\/span><span style=\"color: rgb(0, 0, 255);\">out<\/span><span style=\"color: rgb(0, 0, 0);\">-<\/span><span style=\"color: rgb(0, 0, 255);\">null<\/span><\/div><\/pre>\n<\/div>\n<p>Now if you look at $oops you&#8217;ll see the error message. Any errors are still recorded in the default $error variable. $oops is an error record as you can see:<\/p>\n<div class=\"wlWriterSmartContent\" id=\"57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:d86eb1b8-84fc-49ce-8b81-6ab4bc4edc27\" contenteditable=\"false\" style=\"margin: 0px; padding: 0px; display: inline; float: none;\">\n<pre style=\"background-color: White;\"><div><!--Code highlighting produced by Actipro CodeHighlighter (freeware)http:\/\/www.CodeHighlighter.com\/--><span style=\"color: rgb(0, 0, 0);\">Mon <\/span><span style=\"color: rgb(0, 0, 0);\">1<\/span><span style=\"color: rgb(0, 0, 0);\">\/<\/span><span style=\"color: rgb(0, 0, 0);\">22<\/span><span style=\"color: rgb(0, 0, 0);\">\/<\/span><span style=\"color: rgb(0, 0, 0);\">2007<\/span><span style=\"color: rgb(0, 0, 0);\"> <\/span><span style=\"color: rgb(0, 0, 0);\">12<\/span><span style=\"color: rgb(0, 0, 0);\">:<\/span><span style=\"color: rgb(0, 0, 0);\">25<\/span><span style=\"color: rgb(0, 0, 0);\">:<\/span><span style=\"color: rgb(0, 0, 0);\">15<\/span><span style=\"color: rgb(0, 0, 0);\"> PM PS S:\\ <\/span><span style=\"color: rgb(0, 0, 0);\">&gt;<\/span><span style=\"color: rgb(0, 0, 0);\"> $oops <\/span><span style=\"color: rgb(0, 0, 0);\">|<\/span><span style=\"color: rgb(0, 0, 0);\">gm<br><br><br>   TypeName: System.Management.Automation.ErrorRecord<br><br>Name                      MemberType Definition<br><\/span><span style=\"color: rgb(0, 0, 0);\">----<\/span><span style=\"color: rgb(0, 0, 0);\">                      <\/span><span style=\"color: rgb(0, 0, 0);\">----------<\/span><span style=\"color: rgb(0, 0, 0);\"> <\/span><span style=\"color: rgb(0, 0, 0);\">----------<\/span><span style=\"color: rgb(0, 0, 0);\">Equals                    Method     System.Boolean Equals(Object obj)GetHashCode               Method     System.Int32 GetHashCode()GetObjectData             Method     System.Void GetObjectData(Serialization..GetType                   Method     System.Type GetType()get_CategoryInfo          Method     System.Management.Automation.ErrorCateg..get_ErrorDetails          Method     System.Management.Automation.ErrorDetai..get_Exception             Method     System.Exception get_Exception()get_FullyQualifiedErrorId Method     System.String get_FullyQualifiedErrorId()get_InvocationInfo        Method     System.Management.Automation.Invocation..get_TargetObject          Method     System.Object get_TargetObject()set_ErrorDetails          Method     System.Void set_ErrorDetails(ErrorDetai..ToString                  Method     System.String ToString()CategoryInfo              Property   System.Management.Automation.ErrorCateg..ErrorDetails              Property   System.Management.Automation.ErrorDetai..Exception                 Property   System.Exception Exception {<\/span><span style=\"color: rgb(0, 0, 255);\">get<\/span><span style=\"color: rgb(0, 0, 0);\">;}<br>FullyQualifiedErrorId     Property   System.String FullyQualifiedErrorId {<\/span><span style=\"color: rgb(0, 0, 255);\">get<\/span><span style=\"color: rgb(0, 0, 0);\">;<br>InvocationInfo            Property   System.Management.Automation.Invocation..<br>TargetObject              Property   System.Object TargetObject {<\/span><span style=\"color: rgb(0, 0, 255);\">get<\/span><span style=\"color: rgb(0, 0, 0);\">;}<br><\/span><\/div><\/pre>\n<\/div>\n<p>It&#8217;s not apparent, but this is an array that should only have a single member. So if you wanted to see the exception you would use code like this:<\/p>\n<\/p>\n<div class=\"wlWriterSmartContent\" id=\"57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:229cb1c4-05ef-4990-9137-11a772e84686\" contenteditable=\"false\" style=\"margin: 0px; padding: 0px; display: inline; float: none;\">\n<pre style=\"background-color: White;\"><div><!--Code highlighting produced by Actipro CodeHighlighter (freeware)http:\/\/www.CodeHighlighter.com\/--><span style=\"color: rgb(0, 0, 0);\">$oops[<\/span><span style=\"color: rgb(0, 0, 0);\">0<\/span><span style=\"color: rgb(0, 0, 0);\">].Exception<\/span><\/div><\/pre>\n<\/div>\n<p>If you were adding error handling to handle different error codes you&#8217;ll want to use something like this:<\/p>\n<div class=\"wlWriterSmartContent\" id=\"57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:d0af30ea-d8ac-4546-99a3-d9a3aa9857a0\" contenteditable=\"false\" style=\"margin: 0px; padding: 0px; display: inline; float: none;\">\n<pre style=\"background-color: White;\"><div><!--Code highlighting produced by Actipro CodeHighlighter (freeware)http:\/\/www.CodeHighlighter.com\/--><span style=\"color: rgb(0, 0, 0);\"> ($oops[<\/span><span style=\"color: rgb(0, 0, 0);\">0<\/span><span style=\"color: rgb(0, 0, 0);\">].Exception).errorcode<\/span><\/div><\/pre>\n<\/div>\n<p>For the RPC server unavailable error you would see -2147023174<\/p>\n<p>Defining your own error variables gives you some additional flexibility for your PowerShell scripts and expressions.<\/p>\n<div class=\"wlWriterSmartContent\" id=\"0767317B-992E-4b12-91E0-4F059A8CECA8:83db4239-f6a2-4228-9da2-f7d324316e3a\" contenteditable=\"false\" style=\"margin: 0px; padding: 0px; display: inline;\">Technorati tags: <a href=\"http:\/\/technorati.com\/tags\/PowerShell\" rel=\"tag\">PowerShell<\/a>, <a href=\"http:\/\/technorati.com\/tags\/Variables\" rel=\"tag\">Variables<\/a>, <a href=\"http:\/\/technorati.com\/tags\/Error\" rel=\"tag\">Error<\/a>, <a href=\"http:\/\/technorati.com\/tags\/Scripting\" rel=\"tag\">Scripting<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Another common parameter you might want to include in your PowerShell code is -ErrorVariable. Like -OutVariable that I talked about yesterday, you can redirect error information to a specified variable:gwmi win32_operatingsystem -errorvariable oops |out-null<\/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":[2,25],"tags":[],"class_list":["post-55","post","type-post","status-publish","format-standard","hentry","category-general","category-windows-powershell"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/55","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=55"}],"version-history":[{"count":0,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/55\/revisions"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=55"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=55"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=55"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}