<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Pavel Chuchuva &#187; powershell</title>
	<atom:link href="http://chuchuva.com/pavel/tag/powershell/feed/" rel="self" type="application/rss+xml" />
	<link>http://chuchuva.com/pavel</link>
	<description>Blog about software</description>
	<lastBuildDate>Mon, 07 May 2012 11:22:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>PowerShell Exit Codes</title>
		<link>http://chuchuva.com/pavel/2012/05/powershell-exit-codes/</link>
		<comments>http://chuchuva.com/pavel/2012/05/powershell-exit-codes/#comments</comments>
		<pubDate>Mon, 07 May 2012 11:22:06 +0000</pubDate>
		<dc:creator>Pavel Chuchuva</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[powershell]]></category>

		<guid isPermaLink="false">http://chuchuva.com/pavel/?p=268</guid>
		<description><![CDATA[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 &#8230; <a href="http://chuchuva.com/pavel/2012/05/powershell-exit-codes/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Recently I came across an interesting quirk in PowerShell: exit codes from powershell.exe are inconsistent.</p>
<p>As you may know there are two ways of launching PowerShell script: you can use -File or -Command parameter.</p>
<pre><code>powershell -File test.ps1
</code></pre>
<p>or</p>
<pre><code>powershell -Command .\test.ps1
</code></pre>
<p>-Command could be ommitted:</p>
<pre><code>powershell .\test.ps1
</code></pre>
<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>
<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>
<p>You can confirm this by creating a script that always throws an error:</p>
<pre><code>throw 'oops'
</code></pre>
<p>Now if you launch it using -File parameter exit code is 0:</p>
<pre><code>powershell.exe -File testerror.ps1
echo Error level is %ERRORLEVEL%

oops
At C:\testerror.ps1:1 char:6
+ throw &lt;&lt;&lt;&lt;  'oops'
    + CategoryInfo          : OperationStopped: (oops:String) [], RuntimeException
    + FullyQualifiedErrorId : oops

Error level is 0
</code></pre>
<p>But if you use -Command parameter exit code is set as expected:</p>
<pre><code>powershell.exe -Command .\testerror.ps1
echo Error level is %ERRORLEVEL%
</code></pre>
<p>Chris Oldwood also <a href="http://chrisoldwood.blogspot.com.au/2011/05/powershell-throwing-exceptions-exit.html">came across</a> this bug:</p>
<blockquote>
<p>depending on whether I use the “-File” or “-Command” 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’ve yet to understand?</p>
</blockquote>
<h2>Bonus</h2>
<p>How to execute PowerShell script from NAnt script:</p>
<pre><code>&lt;exec program="powershell.exe" append="true"&gt;
  &lt;arg value="-noprofile" /&gt;
  &lt;arg value="&amp;amp;'C:\your-script.ps1'" /&gt;
  &lt;arg value="param1" /&gt;
  &lt;arg value="'param 2 with spaces'" /&gt;
&lt;/exec&gt;
</code></pre>
<h2>References</h2>
<ul>
<li><a href="http://blogs.msdn.com/b/powershell/archive/2006/10/14/windows-powershell-exit-codes.aspx">Windows PowerShell Exit Codes</a></li>
<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>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://chuchuva.com/pavel/2012/05/powershell-exit-codes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Enhancement to launching PowerShell scripts from Far Manager</title>
		<link>http://chuchuva.com/pavel/2010/03/enhancement-to-launching-powershell-scripts-from-far-manager/</link>
		<comments>http://chuchuva.com/pavel/2010/03/enhancement-to-launching-powershell-scripts-from-far-manager/#comments</comments>
		<pubDate>Fri, 26 Mar 2010 10:16:56 +0000</pubDate>
		<dc:creator>Pavel Chuchuva</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[far-manager]]></category>
		<category><![CDATA[powershell]]></category>

		<guid isPermaLink="false">http://chuchuva.com/pavel/?p=144</guid>
		<description><![CDATA[The method of launching PowerShell scripts from Far Manager I have described previously has 2 important drawbacks: You can&#8217;t pass arguments to the script, and You can&#8217;t launch script in a separate window Here&#8217;s how to fix this: Start regedit, &#8230; <a href="http://chuchuva.com/pavel/2010/03/enhancement-to-launching-powershell-scripts-from-far-manager/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>The method of <a href="http://chuchuva.com/pavel/2009/08/how-to-run-powershell-scripts-from-far-manager/">launching PowerShell scripts from Far Manager</a> I have described previously has 2 important drawbacks:</p>
<ol>
<li>You can&#8217;t pass arguments to the script, and</li>
<li>You can&#8217;t launch script in a separate window</li>
</ol>
<p>Here&#8217;s how to fix this:</p>
<ol>
<li>Start regedit, open HKCR\Microsoft.PowerShellScript.1\shell\Open\command key</li>
<li>Set it to<br />
<code>"C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe" "-file" "%1" %*</code></li>
</ol>
<p>Now you can press Shift+Enter to start PowerShell script in a new window.</p>
<p>As a bonus, you can now double-click .ps1 files in Windows Explorer to launch them. Why is this not a default behaviour? Most likely Microsoft decided to be extra cautious here.</p>
<p>Make sure you have Powershell version 2.0 or higher for this to work. <a href="http://powershell.com/cs/blogs/tips/archive/2009/09/11/which-powershell-version-am-i-running.aspx">How to check Powershell version</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://chuchuva.com/pavel/2010/03/enhancement-to-launching-powershell-scripts-from-far-manager/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>How to redirect output of console program to a file in PowerShell</title>
		<link>http://chuchuva.com/pavel/2010/03/how-to-redirect-output-of-console-program-to-a-file-in-powershell/</link>
		<comments>http://chuchuva.com/pavel/2010/03/how-to-redirect-output-of-console-program-to-a-file-in-powershell/#comments</comments>
		<pubDate>Fri, 05 Mar 2010 09:10:44 +0000</pubDate>
		<dc:creator>Pavel Chuchuva</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[powershell]]></category>

		<guid isPermaLink="false">http://chuchuva.com/pavel/?p=134</guid>
		<description><![CDATA[At some stage I decided I need backup plan for my virtual server. After some intensive web surfing I found brilliant command line tool &#8211; s3.exe. The next step was to schedule to run it periodically, every night, when everybody &#8230; <a href="http://chuchuva.com/pavel/2010/03/how-to-redirect-output-of-console-program-to-a-file-in-powershell/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>At some stage I decided I need backup plan for my virtual server. After some intensive web surfing I found brilliant command line tool &#8211; <a href="http://s3.codeplex.com/">s3.exe</a>. The next step was to schedule to run it periodically, every night, when everybody is sleeping.</p>
<p><img src="http://chuchuva.com/pavel/images/2010/03/night-mystery.jpg" alt="Night" title="" width="500" height="375" class="alignnone size-full wp-image-140" /></p>
<p>I decided to use PowerShell. It&#8217;s new, it&#8217;s powerful, it comes pre-installed with Windows Server 2008. PowerShell is very popular among system administrators.</p>
<p>Also, I got an idea to save output of backup application to log file so that I could see results later. And this is where things didn&#8217;t work as I expected. Here&#8217;s how to redirect console program output to a file:</p>
<pre><code>s3.exe &gt; log.txt</code></pre>
<p>If you run this you will be surprised to find that log.txt is empty. As it turns out s3.exe, being a good Windows citizen, writes errors to STDERR stream. To redirect error output to the same file you need to add magic string 2&gt;&amp;1:</p>
<pre><code>s3.exe 2&gt;&amp;1 &gt; log.txt</code></pre>
<p>Two new problems appear. First, errors appear as exceptions in log file:</p>
<pre><code>s3.exe : s3.exe version 1.6 - check for updates at http://s3.codeplex.com
At PS Logging.ps1:4 char:43
+ s3.exe &lt;&lt;&lt;&lt;  2&gt;&amp;1 &gt; log.txt
    + CategoryInfo          : NotSpecified: (s3.exe version ...s3.codeplex.com
   :String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError</code></pre>
<p>PowerShell helpfully wraps STDERR output <a href="http://halr9000.com/article/837">into exceptions</a>. This is nice but I don&#8217;t need it! All I want is to see how a program worked by examining log file. Do this to unwrap exceptions and show plain text:</p>
<pre><code>s3.exe 2&gt;&amp;1 | foreach-object {$_.ToString()} | Out-File log.txt</code></pre>
<p>And second, errors areÂ out of order with normal output. They could appear before or after normal lines emitted by a program, making it hard to diagnose problem. To confirm that, I created a simple console program:</p>
<pre><code class="prettyprint">static void Main(string[] args)
{
   for (int i = 0; i < 10; i++)
   {
      Console.WriteLine("Hello");
   }
   Console.Error.WriteLine("Bah bah");
   for (int i = 0; i < 10; i++)
   {
      Console.WriteLine("2");
   }
}
</code></pre>
<p>If you run this program from PowerShell and redirect output to a file, error message (bah bah) could appear anywhere in the log file:</p>
<pre><code>Hello
<strong>Bah bah</strong>
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
2
2
2
2
2
2
2
2
2
2</code></pre>
<p> The solution I came up with is to let old reliable cmd.exe to do the work:</p>
<pre><code>cmd /c s3.exe `>log.txt 2`>`&#038;1</code></pre>
<p>Note backsticks â€“ I use them to prevent PowerShell from parsing redirect operators.</p>
<h4>Links</h4>
<p><a href="http://devcentral.f5.com/weblogs/Joe/archive/2009/01/09/powershell-abcs---o-is-for-output.aspx">PowerShell ABC's - O is for Output</a><br />
<a href="http://groups.google.com.au/group/microsoft.public.windows.powershell/browse_thread/thread/41c84d2b76785329/228e5ee428392294">How to capture exe output to a PowerShell variable</a><br />
Another approach to unwrap exceptions: <a href="http://knowbody.livejournal.com/11406.html">use add-content cmdlet</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://chuchuva.com/pavel/2010/03/how-to-redirect-output-of-console-program-to-a-file-in-powershell/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to run PowerShell scripts from Far Manager</title>
		<link>http://chuchuva.com/pavel/2009/08/how-to-run-powershell-scripts-from-far-manager/</link>
		<comments>http://chuchuva.com/pavel/2009/08/how-to-run-powershell-scripts-from-far-manager/#comments</comments>
		<pubDate>Wed, 05 Aug 2009 10:27:21 +0000</pubDate>
		<dc:creator>Pavel Chuchuva</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[far-manager]]></category>
		<category><![CDATA[powershell]]></category>

		<guid isPermaLink="false">http://chuchuva.com/pavel/?p=34</guid>
		<description><![CDATA[Let&#8217;s say you use Far Manager and want to launch PowerShell script. With default installation pressing Enter on PowerShell script in Far Manager will open the script in Notepad. Not very helpful. Here&#8217;s how to configure Far Manager to launch &#8230; <a href="http://chuchuva.com/pavel/2009/08/how-to-run-powershell-scripts-from-far-manager/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Let&#8217;s say you use Far Manager and want to launch PowerShell script. With default installation pressing Enter on PowerShell script in Far Manager will open the script in Notepad. Not very helpful.</p>
<p><img class="alignnone size-full wp-image-35" src="http://chuchuva.com/pavel/wp-content/uploads/2009/08/notepad.png" alt="" width="274" height="143" /></p>
<p>Here&#8217;s how to configure Far Manager to launch PowerShell script by pressing Enter key:</p>
<ol>
<li>Open Commands menu, select File associations.</li>
<li>Press Ins key to add new association.</li>
<li>Enter *.ps1 as file mask.</li>
<li>Enter the following as execute command:
<pre>powershell.exe -file "!.!"</pre>
</li>
</ol>
<p><img class="alignnone size-full wp-image-37" src="http://chuchuva.com/pavel/wp-content/uploads/2009/08/file-association.gif" alt="" width="498" height="232" /></p>
<p>Bonus: how to change colour of PowerShell scripts to green:</p>
<ol>
<li>Open Options menu, select Files highlighting.</li>
<li>Locate *.exe,*.com,*.bat entry, press Enter to edit.</li>
<li>Add *.ps1 to the list, select Ok to save.</li>
</ol>
<p><img class="alignnone size-full wp-image-38" src="http://chuchuva.com/pavel/wp-content/uploads/2009/08/powershell-far.png" alt="" width="403" height="181" /></p>
<p>See also: <a href="http://www.microsoft.com/technet/scriptcenter/topics/winpsh/manual/run.mspx">Running Windows PowerShell Scripts</a></p>
<p><strong>Update</strong>: there is an <a href="http://chuchuva.com/pavel/2010/03/enhancement-to-launching-powershell-scripts-from-far-manager/">enhancement</a> that allows passing arguments to the script and launch script in a separate window.</p>
]]></content:encoded>
			<wfw:commentRss>http://chuchuva.com/pavel/2009/08/how-to-run-powershell-scripts-from-far-manager/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

