{"id":13711,"date":"2011-06-06T00:01:00","date_gmt":"2011-06-06T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/06\/06\/get-legacy-exit-codes-in-powershell\/"},"modified":"2011-06-06T00:01:00","modified_gmt":"2011-06-06T00:01:00","slug":"get-legacy-exit-codes-in-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/get-legacy-exit-codes-in-powershell\/","title":{"rendered":"Get Legacy Exit Codes in PowerShell"},"content":{"rendered":"<p><b>Summary<\/b>: Learn how to get exit codes from legacy programs or VBScript script from inside Windows PowerShell.\nMicrosoft Scripting Guy, Ed Wilson, here. This week we will have one guest blogger for the entire week. Sean Kearney has written a series of blog posts about Windows PowerShell and the Legacy. I am not going to be redundant by reposting <a target=\"_blank\" href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/04\/25\/expert-solution-for-2011-scripting-games-beginner-event-6-parse-the-windows-update-log-for-errors-with-powershell.aspx\">his biography<\/a> each and every day.<\/p>\n<h3>Integrating Windows PowerShell with Legacy Environments&ndash;Part 1<\/h3>\n<p>The class gathers in as the Scripting Guy begins to speak&hellip;a thousand eager minds ready to be filled with knowledge.\nThe great one&mdash;the Scripting Guy, steps up to the podium and begins with a quiet easy drawl, &ldquo;So today, class, we&rsquo;re going to dig deeper into the mysteries of Windows PowerShell, write ourselves some cool advanced functions, play a little with regular expressions, perhaps write a module&hellip;&rdquo;\nA small, shrill voice pipes up, &ldquo;Oh! Oh! Oh! Oh!&rdquo;\nThe Master pauses and looks over. A disheveled and somewhat fidgety little man at the back of the room is wildly jumping up and down with his arms waving&hellip;\n&ldquo;Oh! Oh! Oh!&rdquo; the little creature continues.\nThe Scripting Guy, always eager to encourage interest, politely nods to encourage the young one to finish his question.\n&ldquo;Scripting Guy!&rdquo; he pipes up eagerly. &ldquo;I love Windows PowerShell, but I read on the Hey! Scripting Guy blog that Windows Powershell will <a target=\"_blank\" href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/07\/08\/hey-scripting-guy-awww-windows-powershell-plays-nice-nice-with-vbscript-and-batch.aspx\">play nice with VBScript<\/a>!&nbsp;Could you tell us more about this? Nobody documents it online. There&rsquo;s no FAQs, it&rsquo;s&hellip;it&rsquo;s unbelieved! Please tell us more!&rdquo;\nThe Scripting Guy paused, &ldquo;And who are you, so eager to learn the mysteries of the Legacy and Windows PowerShell?&rdquo;\n&ldquo;They call me Sean. Please tell me more, O Master of Scripting.&rdquo;\nThe Scripting Guy nods. &ldquo;And so we shall&hellip;&rdquo; he stated as he invoked the following cmdlet.<\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 7.5pt 0in\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">GET-CONTENT C:PowershellLegacy1.txt | more<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>One of the misconceptions that people have about working with script, console applications, and Windows PowerShell is that there is no way to pass parameters and receive exit codes. I once believed this to be true as well. But Windows PowerShell is truly a &ldquo;powerful&rdquo; shell because it does not destroy what we already have.\nWithin console applications and VBScript scripts, normally we would receive the error that was last reported by the following variable:<\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 7.5pt 0in\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">%ERRORLEVEL%<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>Or VBScript would pass back a variable such as <b>ThisVariable <\/b>after we invoke an application like the following.<\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 7.5pt 0in\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">DIM OBJSHELL=Wscript.NewObject(&ldquo;Wscript.Shell&rdquo;) <\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 7.5pt 0in\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">APP=&ldquo;CMD.EXE \/C PROGRAM.CMD&ldquo; <\/span><\/span><\/span><\/p>\n<p>  <span style=\"line-height: normal;list-style-type: disc\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">THISVARIABLE=OBJSHELL.RUN(APP)<\/span><\/span><\/span><\/p><\/blockquote>\n<p>Now if we tried this in Windows PowerShell, we would be stymied because <b>$ErrorLevel<\/b> doesn&rsquo;t exist, accessing <b>$ENV:ERRORLEVEL<\/b> doesn&rsquo;t work worth a darn, and calling a console application like the following only returns the results from the screen as an object.&nbsp;<\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 7.5pt 0in\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">$THISVARIABLE=(&amp; &lsquo;PROGRAM.CMD&rsquo;) <\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>So you stare at the Windows PowerShell console helplessly, frustrated, and crying for a solution to beings who won&rsquo;t listen&hellip;not knowing that the answer is in front of you the whole time.\nWithin Windows PowerShell, there is a variable called <b>$LastExitCode<\/b>.\nYes, it&rsquo;s&hellip;\n(Wait for it&hellip;wait for it&hellip;pause for effect&hellip;) the Windows PowerShell equivalent of <b>%ERRORLEVEL%<\/b>. The wonderful part is that it works exactly the same. If you invoke a console application or a VBScript script that would return something to <b>%ERRORLEVEL%<\/b>, from within Windows PowerShell, you&rsquo;ll get the response in <b>$LastExitCode<\/b>.\nNow it gets better. If you call Windows PowerShell scripts from a console application or a VBScript script as follows, they also return a value.<\/p>\n<blockquote><p>  <span style=\"line-height: normal;list-style-type: disc\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">POWERSHELL.EXE &ndash;FILE Scriptname.PS1 &ndash;executionpolicy RemoteSigned <\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>By default, if it doesn&rsquo;t throw an error, it will return 0; or it will return 1 if something bad happens.\nNow also take note, folks. If within a Windows PowerShell script, you execute a line such as the following, that value will also be returned to the console application or a VBScript script as normal.<\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 7.5pt 0in\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">EXIT 42 <\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 7.5pt 0in\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">or <\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;list-style-type: disc;margin: 7.5pt 0in\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\">RETURN 24 <\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>&nbsp;The Scripting Guy looks at the back of the room as the text file ends on the screen.&nbsp;\nThe little fuzzy haired one is chomping away eagerly on a box of popcorn. The floor is littered with kernels. &ldquo;More! More, more, MORE!&rdquo; he bursts with popcorn flying in the air.\n&ldquo;Young one, that is for another blog post. For now you must learn patience. Tomorrow we will discuss passing variables to Windows PowerShell from a console application or a VBScript script.&rdquo;\nThe little one unhappily slumps back in his chair, chewing on a Rubik&rsquo;s Cube.\nGuest blogger week will continue tomorrow when Sean will continue to talk about Windows PowerShell and the Legacy. A special thank you to Sean for writing this week&rsquo;s blog posts. Hope you enjoy them.\nI invite you to follow me on <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> and <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to get exit codes from legacy programs or VBScript script from inside Windows PowerShell. Microsoft Scripting Guy, Ed Wilson, here. This week we will have one guest blogger for the entire week. Sean Kearney has written a series of blog posts about Windows PowerShell and the Legacy. I am not going to [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[56,2,3,4,154,155,45],"class_list":["post-13711","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-guest-blogger","tag-running","tag-scripting-guy","tag-scripting-techniques","tag-sean-kearney","tag-vbscript-migration","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to get exit codes from legacy programs or VBScript script from inside Windows PowerShell. Microsoft Scripting Guy, Ed Wilson, here. This week we will have one guest blogger for the entire week. Sean Kearney has written a series of blog posts about Windows PowerShell and the Legacy. I am not going to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/13711","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=13711"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/13711\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=13711"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=13711"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=13711"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}