{"id":268,"date":"2012-05-07T21:22:06","date_gmt":"2012-05-07T11:22:06","guid":{"rendered":"http:\/\/chuchuva.com\/pavel\/?p=268"},"modified":"2012-05-07T09:30:58","modified_gmt":"2012-05-06T23:30:58","slug":"powershell-exit-codes","status":"publish","type":"post","link":"https:\/\/chuchuva.com\/pavel\/2012\/05\/powershell-exit-codes\/","title":{"rendered":"PowerShell Exit Codes"},"content":{"rendered":"<p>Recently I came across an interesting quirk in PowerShell: exit codes from powershell.exe are inconsistent.<\/p>\n<p>As you may know there are two ways of launching PowerShell script: you can use -File or -Command parameter.<\/p>\n<pre><code>powershell -File test.ps1\r\n<\/code><\/pre>\n<p>or<\/p>\n<pre><code>powershell -Command .\\test.ps1\r\n<\/code><\/pre>\n<p>-Command could be ommitted:<\/p>\n<pre><code>powershell .\\test.ps1\r\n<\/code><\/pre>\n<p>Now let&#8217;s say you want to execute PowerShell script from a scheduled task or from continuous integration server. Naturally, you would be interested if the script fails or not.<\/p>\n<p>And here&#8217;s the bug: if you launch your script using -File parameter powershell.exe exits with zero code even if there was an exception.<\/p>\n<p>You can confirm this by creating a script that always throws an error:<\/p>\n<pre><code>throw 'oops'\r\n<\/code><\/pre>\n<p>Now if you launch it using -File parameter exit code is 0:<\/p>\n<pre><code>powershell.exe -File testerror.ps1\r\necho Error level is %ERRORLEVEL%\r\n\r\noops\r\nAt C:\\testerror.ps1:1 char:6\r\n+ throw &lt;&lt;&lt;&lt;  'oops'\r\n    + CategoryInfo          : OperationStopped: (oops:String) [], RuntimeException\r\n    + FullyQualifiedErrorId : oops\r\n\r\nError level is 0\r\n<\/code><\/pre>\n<p>But if you use -Command parameter exit code is set as expected:<\/p>\n<pre><code>powershell.exe -Command .\\testerror.ps1\r\necho Error level is %ERRORLEVEL%\r\n<\/code><\/pre>\n<p>Chris Oldwood also <a href=\"http:\/\/chrisoldwood.blogspot.com.au\/2011\/05\/powershell-throwing-exceptions-exit.html\">came across<\/a> this bug:<\/p>\n<blockquote>\n<p>depending on whether I use the \u201c-File\u201d or \u201c-Command\u201d switch to execute the .ps1 script I get different behaviour. Is this a PowerShell bug or is there something fundamental about the execution model the differs between -File and -Command that I\u2019ve yet to understand?<\/p>\n<\/blockquote>\n<h2>Bonus<\/h2>\n<p>How to execute PowerShell script from NAnt script:<\/p>\n<pre><code>&lt;exec program=\"powershell.exe\" append=\"true\"&gt;\r\n  &lt;arg value=\"-noprofile\" \/&gt;\r\n  &lt;arg value=\"&amp;amp;'C:\\your-script.ps1'\" \/&gt;\r\n  &lt;arg value=\"param1\" \/&gt;\r\n  &lt;arg value=\"'param 2 with spaces'\" \/&gt;\r\n&lt;\/exec&gt;\r\n<\/code><\/pre>\n<h2>References<\/h2>\n<ul>\n<li><a href=\"http:\/\/blogs.msdn.com\/b\/powershell\/archive\/2006\/10\/14\/windows-powershell-exit-codes.aspx\">Windows PowerShell Exit Codes<\/a><\/li>\n<li><a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/06\/06\/get-legacy-exit-codes-in-powershell.aspx\">Get Legacy Exit Codes in PowerShell<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Recently I came across an interesting quirk in PowerShell: exit codes from powershell.exe are inconsistent. As you may know there are two ways of launching PowerShell script: you can use -File or -Command parameter. powershell -File test.ps1 or powershell -Command .\\test.ps1 -Command could be ommitted: powershell .\\test.ps1 Now let&#8217;s say you want to execute PowerShell &hellip; <a href=\"https:\/\/chuchuva.com\/pavel\/2012\/05\/powershell-exit-codes\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">PowerShell Exit Codes<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[7],"tags":[10],"class_list":["post-268","post","type-post","status-publish","format-standard","hentry","category-software","tag-powershell"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/chuchuva.com\/pavel\/wp-json\/wp\/v2\/posts\/268","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/chuchuva.com\/pavel\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chuchuva.com\/pavel\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chuchuva.com\/pavel\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/chuchuva.com\/pavel\/wp-json\/wp\/v2\/comments?post=268"}],"version-history":[{"count":0,"href":"https:\/\/chuchuva.com\/pavel\/wp-json\/wp\/v2\/posts\/268\/revisions"}],"wp:attachment":[{"href":"https:\/\/chuchuva.com\/pavel\/wp-json\/wp\/v2\/media?parent=268"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chuchuva.com\/pavel\/wp-json\/wp\/v2\/categories?post=268"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chuchuva.com\/pavel\/wp-json\/wp\/v2\/tags?post=268"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}