<?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>Vidar's Musings</title>
	<atom:link href="http://www.kongsli.net/nblog/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kongsli.net/nblog</link>
	<description>Deep thoughts on shallow topics</description>
	<lastBuildDate>Thu, 25 Apr 2013 08:34:59 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Powershell gotchas: refer to variables outside a function</title>
		<link>http://www.kongsli.net/nblog/2013/04/25/powershell-gotchas-refer-to-variables-outside-a-function/</link>
		<comments>http://www.kongsli.net/nblog/2013/04/25/powershell-gotchas-refer-to-variables-outside-a-function/#comments</comments>
		<pubDate>Thu, 25 Apr 2013 08:34:59 +0000</pubDate>
		<dc:creator>vidarkongsli</dc:creator>
				<category><![CDATA[Microsoft technologies]]></category>
		<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Software development]]></category>
		<category><![CDATA[gotcha]]></category>
		<category><![CDATA[powershell gotcha]]></category>
		<category><![CDATA[powershell_gotchas]]></category>

		<guid isPermaLink="false">http://www.kongsli.net/nblog/?p=485</guid>
		<description><![CDATA[Consider the following code: $x = 1 function changeit { "Changing &#96;$x. Was: $x" $x = 2 "New value: $x" } "&#96;$x has value $x" changeit "&#96;$x has value $x" Not too complicated, right. Let&#8217;s see what happens when we &#8230; <a href="http://www.kongsli.net/nblog/2013/04/25/powershell-gotchas-refer-to-variables-outside-a-function/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Consider the following code:</p>

<p><pre class="csharp" name="code">$x = 1
function changeit {
    "Changing &#96;$x. Was: $x"
    $x = 2
    "New value: $x"
}
"&#96;$x has value $x"
changeit
"&#96;$x has value $x"</pre></p>

<p>Not too complicated, right. Let&#8217;s see what happens when we run it:</p>

<p><pre class="csharp" name="code">$x has value 1
Changing $x. Was: 1
New value: 2
$x has value 1</pre></p>

<p>What&#8217;s going on? Well, trying to change the value of $x inside the function did not work. Why not? What the statement <code>$x = 2</code> actually does, is to create a local variable inside the function and give it the value of 2. Let&#8217;s fix that:</p>

<p><pre class="csharp"  name="code">$x = 1
function changeit {
    "Changing &#96;$x. Was: $x"
    $script:x = 2
    "New value: $x"
}
"&#96;$x has value $x"
changeit
"&#96;$x has value $x"</pre></p>

<p>Now, we run it again:</p>

<p><pre class="csharp" name="code">$x has value 1
Changing $x. Was: 1
New value: 2
$x has value 2</pre></p>

<p>The thing is that we have to explicitly tell Powershell to update the variable in the parent scope instead of creating a new variable in the current scope.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.kongsli.net%2Fnblog%2F2013%2F04%2F25%2Fpowershell-gotchas-refer-to-variables-outside-a-function%2F&amp;title=Powershell%20gotchas%3A%20refer%20to%20variables%20outside%20a%20function" id="wpa2a_2"><img src="http://www.kongsli.net/nblog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.kongsli.net/nblog/2013/04/25/powershell-gotchas-refer-to-variables-outside-a-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Windows 8 Store: må ha en-US i listen av språk på maskinen</title>
		<link>http://www.kongsli.net/nblog/2012/09/13/windows-8-store-ma-ha-en-us-i-listen-av-sprak-pa-maskinen/</link>
		<comments>http://www.kongsli.net/nblog/2012/09/13/windows-8-store-ma-ha-en-us-i-listen-av-sprak-pa-maskinen/#comments</comments>
		<pubDate>Thu, 13 Sep 2012 11:25:48 +0000</pubDate>
		<dc:creator>vidarkongsli</dc:creator>
				<category><![CDATA[Microsoft technologies]]></category>
		<category><![CDATA[Windows 8]]></category>
		<category><![CDATA[language]]></category>
		<category><![CDATA[windows 8]]></category>
		<category><![CDATA[windows store]]></category>

		<guid isPermaLink="false">http://www.kongsli.net/nblog/?p=479</guid>
		<description><![CDATA[(This post is in Norwegian. For a short English summary, scroll to the bottom of the post) Når du installerer Windows 8, er det viktig at du har installert en-US som språk, og ikke bare norsk. Hvis ikke, vil Windows &#8230; <a href="http://www.kongsli.net/nblog/2012/09/13/windows-8-store-ma-ha-en-us-i-listen-av-sprak-pa-maskinen/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>(This post is in Norwegian. For a short English summary, scroll to the bottom of the post)</p>

<p>Når du installerer Windows 8, er det viktig at du har installert en-US som språk, og ikke bare norsk. Hvis ikke, vil Windows Store være så og si tom når du browser eller søker i den. For å finne oppsettet trykk Win, Win + F, søk etter &#8220;language&#8221; i &#8220;settings&#8221;.</p>

<p><a href="http://www.kongsli.net/nblog/wp-content/uploads/2012/09/Language-settings.png"><img class="alignnone size-full wp-image-480" title="Language-settings" src="http://www.kongsli.net/nblog/wp-content/uploads/2012/09/Language-settings.png" alt="" width="823" height="584" /></a></p>

<p>Uten dette, er det fint lite å finne i Windows Store (per september 2012):</p>

<p><a href="http://www.kongsli.net/nblog/wp-content/uploads/2012/09/No-apps.png"><img class="alignnone size-full wp-image-481" title="No-apps" src="http://www.kongsli.net/nblog/wp-content/uploads/2012/09/No-apps.png" alt="" width="685" height="312" /></a></p>

<p>(Summary in English: if you are not native to the US, make sure you have en-US installed as a language in Windows 8. It does not have to be the preferred language. Otherwise, the Windows Store will probably have very little apps &#8211; only apps that explicitly supports your native language/culture)</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.kongsli.net%2Fnblog%2F2012%2F09%2F13%2Fwindows-8-store-ma-ha-en-us-i-listen-av-sprak-pa-maskinen%2F&amp;title=Windows%208%20Store%3A%20m%C3%A5%20ha%20en-US%20i%20listen%20av%20spr%C3%A5k%20p%C3%A5%20maskinen" id="wpa2a_4"><img src="http://www.kongsli.net/nblog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.kongsli.net/nblog/2012/09/13/windows-8-store-ma-ha-en-us-i-listen-av-sprak-pa-maskinen/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Powershell gotchas: calling a function</title>
		<link>http://www.kongsli.net/nblog/2012/06/12/powershell-gotchas-calling-a-function/</link>
		<comments>http://www.kongsli.net/nblog/2012/06/12/powershell-gotchas-calling-a-function/#comments</comments>
		<pubDate>Tue, 12 Jun 2012 12:52:30 +0000</pubDate>
		<dc:creator>vidarkongsli</dc:creator>
				<category><![CDATA[Microsoft technologies]]></category>
		<category><![CDATA[Powershell]]></category>
		<category><![CDATA[powershell]]></category>
		<category><![CDATA[powershell_gotchas]]></category>

		<guid isPermaLink="false">http://www.kongsli.net/nblog/?p=463</guid>
		<description><![CDATA[Consider the following code: function f($a, $b, $c) { "f: &#96;$a=$a, &#96;$a.GetType()=$($a.GetType())" "f: &#96;$b=$b, &#96;$b.GetType()=$($b.GetType())" "f: &#96;$c=$c, &#96;$c.GetType()=$($c.GetType())" } f 1 2 3 # call f with three integers as parameters f 1,2,3 # call f with an array of &#8230; <a href="http://www.kongsli.net/nblog/2012/06/12/powershell-gotchas-calling-a-function/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Consider the following code:</p>

<p><pre class="csharp" name="code">function f($a, $b, $c) {
    "f: &#96;$a=$a, &#96;$a.GetType()=$($a.GetType())"
    "f: &#96;$b=$b, &#96;$b.GetType()=$($b.GetType())"
    "f: &#96;$c=$c, &#96;$c.GetType()=$($c.GetType())"
}
f 1 2 3   # call f with three integers as parameters
f 1,2,3   # call f with an array of three integers
f (1,2,3) # as above</pre></p>

<p>The gotcha here is not to fall into the trap to use a C#-like or Java(Script)-like syntax where parameters are separated by commas. The comma in Powershell is always an array constructor operator, and takes precedence over function application. (Which also means that adding paranthesis will yield the same result). The code above will result in:</p>

<p><pre>f: $a=1, $a.GetType()=int
f: $b=2, $b.GetType()=int
f: $c=3, $c.GetType()=int
f: $a=1 2 3, $a.GetType()=System.Object[]
You cannot call a method on a null-valued expression.
At ...\function-application.ps1:4 char:11
+ $b.GetType &lt;&lt;&lt;&lt; ()
    + CategoryInfo          : InvalidOperation: (GetType:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
f: $b=, $b.GetType()=
You cannot call a method on a null-valued expression.
At ...\function-application.ps1:5 char:11
+ $c.GetType &lt;&lt;&lt;&lt; ()
    + CategoryInfo          : InvalidOperation: (GetType:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
f: $c=, $c.GetType()=
f: $a=1 2 3, $a.GetType()=System.Object[]
You cannot call a method on a null-valued expression.
At ...\function-application.ps1:4 char:11
+ $b.GetType &lt;&lt;&lt;&lt; ()
    + CategoryInfo          : InvalidOperation: (GetType:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
f: $b=, $b.GetType()=
You cannot call a method on a null-valued expression.
At ...\function-application.ps1:5 char:11
+ $c.GetType &lt;&lt;&lt;&lt; ()
    + CategoryInfo          : InvalidOperation: (GetType:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
f: $c=, $c.GetType()=</pre></p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.kongsli.net%2Fnblog%2F2012%2F06%2F12%2Fpowershell-gotchas-calling-a-function%2F&amp;title=Powershell%20gotchas%3A%20calling%20a%20function" id="wpa2a_6"><img src="http://www.kongsli.net/nblog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.kongsli.net/nblog/2012/06/12/powershell-gotchas-calling-a-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Powershell gotchas: xml.save needs an absolute path</title>
		<link>http://www.kongsli.net/nblog/2012/05/25/powershell-gotchas-xml-save-needs-an-absolute-path/</link>
		<comments>http://www.kongsli.net/nblog/2012/05/25/powershell-gotchas-xml-save-needs-an-absolute-path/#comments</comments>
		<pubDate>Fri, 25 May 2012 09:52:23 +0000</pubDate>
		<dc:creator>vidarkongsli</dc:creator>
				<category><![CDATA[Microsoft technologies]]></category>
		<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Software development]]></category>
		<category><![CDATA[gotcha]]></category>
		<category><![CDATA[powershell gotcha]]></category>

		<guid isPermaLink="false">http://www.kongsli.net/nblog/?p=408</guid>
		<description><![CDATA[If given a relative path, xml.Save will do nothing, giving no error messages or anything. $xml = [xml]'&#60;root&#62;&#60;item&#62;1&#60;/item&#62;&#60;/root&#62;' $xml.Save('.\foo.xml') # Outputs nothing. $xml.Save((join-path (pwd) -ChildPath '.\foo.xml')) # Actually writes]]></description>
				<content:encoded><![CDATA[<p>If given a relative path, xml.Save will do nothing, giving no error messages or anything.
<pre class="csharp" name="code">$xml = [xml]'&lt;root&gt;&lt;item&gt;1&lt;/item&gt;&lt;/root&gt;'
$xml.Save('.\foo.xml')                              # Outputs nothing.
$xml.Save((join-path (pwd) -ChildPath '.\foo.xml')) # Actually writes
</pre></p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.kongsli.net%2Fnblog%2F2012%2F05%2F25%2Fpowershell-gotchas-xml-save-needs-an-absolute-path%2F&amp;title=Powershell%20gotchas%3A%20xml.save%20needs%20an%20absolute%20path" id="wpa2a_8"><img src="http://www.kongsli.net/nblog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.kongsli.net/nblog/2012/05/25/powershell-gotchas-xml-save-needs-an-absolute-path/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Powershell gotchas: standard out breaks lines to fit console</title>
		<link>http://www.kongsli.net/nblog/2012/05/14/powershell-gotchas-standard-out-breaks-lines-to-fit-console/</link>
		<comments>http://www.kongsli.net/nblog/2012/05/14/powershell-gotchas-standard-out-breaks-lines-to-fit-console/#comments</comments>
		<pubDate>Mon, 14 May 2012 10:52:54 +0000</pubDate>
		<dc:creator>vidarkongsli</dc:creator>
				<category><![CDATA[Microsoft technologies]]></category>
		<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Software development]]></category>
		<category><![CDATA[console]]></category>
		<category><![CDATA[powershell]]></category>
		<category><![CDATA[powershell gotcha]]></category>
		<category><![CDATA[teamcity]]></category>

		<guid isPermaLink="false">http://www.kongsli.net/nblog/?p=446</guid>
		<description><![CDATA[When Powershell writes to standard out, and there is now redirection in the Powershell script itself, it will assume that it writes to a console. Because of this, it kindly breaks lines if they are longer than 80 characters. So, &#8230; <a href="http://www.kongsli.net/nblog/2012/05/14/powershell-gotchas-standard-out-breaks-lines-to-fit-console/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>When Powershell writes to standard out, and there is now redirection in the Powershell script itself, it will assume that it writes to a console. Because of this, it kindly breaks lines if they are longer than 80 characters. So, given the following powershell file (.ps1):</p>

<p><pre name="code" class="csharp"># line-gunk.ps1
"123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"</pre></p>

<p>If you run this Powershell file from the Windows command line, and redirects output to a file:</p>

<p><pre name="code" class="csharp">C:\&gt;@powershell -file .\line-gunk.ps1 &gt; foo.txt</pre></p>

<p>Then you will get:</p>

<p><pre>1234567890123456789012345678901234567890123456789012345678901234567890123456789
01234567890</pre></p>

<p></p>

<p>However, if you do the redirection in Powershell, it&#8217;s fine:</p>

<p><pre name="code" class="csharp">PS C:\&gt; .\line-gunk.ps1 &gt; bar.txt</pre></p>

<p>A näive fix to this, would be to change your script to use <code>Write-Host</code> to explicity write the text to the console. However, I consider this a bad practice because it would restrict the usage of your script as you could not manipulate the output further if reusing the script. I found this entry on stackoverflow which provides a better solution. Going back to the original script, here is a fixed one that worked for me:</p>

<p><pre name="code" class="csharp"># line-gunk-fixed.ps1
$Host.UI.RawUI.BufferSize = New-Object Management.Automation.Host.Size(500, 25)
"123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"</pre></p>

<p>Notice that I used the simplifed version from the stackoverflow entry, which might not work in all circumstances.</p>

<h2>Backstory</h2>

<p>How did I come across this problem? I running Powershell build scripts in <a href="http://www.jetbrains.com/teamcity/">TeamCity</a> and I was trying to <a href="http://confluence.jetbrains.net/display/TCD65/Build+Script+Interaction+with+TeamCity#BuildScriptInteractionwithTeamCity-ReportingBuildProgress">report the build progress</a> back to <a href="http://www.jetbrains.com/teamcity/">TeamCity</a>. However, it somehow did not work. I then discovered that the status messages written by my script were broken by the line breaks, rendering them useless&#8230;</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.kongsli.net%2Fnblog%2F2012%2F05%2F14%2Fpowershell-gotchas-standard-out-breaks-lines-to-fit-console%2F&amp;title=Powershell%20gotchas%3A%20standard%20out%20breaks%20lines%20to%20fit%20console" id="wpa2a_10"><img src="http://www.kongsli.net/nblog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.kongsli.net/nblog/2012/05/14/powershell-gotchas-standard-out-breaks-lines-to-fit-console/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Running chocolatey behind an authenticating firewall</title>
		<link>http://www.kongsli.net/nblog/2012/05/07/running-chocolatey-behind-an-authenticating-firewall/</link>
		<comments>http://www.kongsli.net/nblog/2012/05/07/running-chocolatey-behind-an-authenticating-firewall/#comments</comments>
		<pubDate>Mon, 07 May 2012 10:36:03 +0000</pubDate>
		<dc:creator>vidarkongsli</dc:creator>
				<category><![CDATA[Microsoft technologies]]></category>
		<category><![CDATA[Powershell]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[.net4]]></category>
		<category><![CDATA[407]]></category>
		<category><![CDATA[chocolatey]]></category>
		<category><![CDATA[powershell]]></category>
		<category><![CDATA[proxy authentication]]></category>

		<guid isPermaLink="false">http://www.kongsli.net/nblog/?p=453</guid>
		<description><![CDATA[I long grappled with a problem installing applications using chocolatey. Specifically, all installations that required downloading an MSI (or similar) file outside the .nupkg caused the following error message: The remote server returned an error: (407) Proxy Authentication Required. Yes, &#8230; <a href="http://www.kongsli.net/nblog/2012/05/07/running-chocolatey-behind-an-authenticating-firewall/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>I long grappled with a problem installing applications using <a href="http://www.chocolatey.org">chocolatey</a>. Specifically, all installations that required downloading an MSI (or similar) file outside the <code>.nupkg</code> caused the following error message:</p>

<p><pre>The remote server returned an error: (407) Proxy Authentication Required.</pre></p>

<p>Yes, I am behind a firewall that requires authentication. I found several references to this error message on the chocolatey forums, but they all seemed to be fixed in the latest chocolatey version.</p>

<p>Through some googling I was able to track down the problem. Acutally, it is <a href="http://knowledgebase.solarwinds.com/kb/questions/3226/You+receive+error+407+when+using+McAfee+Web+Gateway.">a problem with the .NET 3.5 runtime</a> that causes the <code>System.Net.WebClient</code> to give up on an NTLM authentication challenge from proxies. Chocolatey uses Powershell, and indeed the <code>System.Net.WebClient</code> to download installation packages (all though not the package manifest itself, where it uses NuGet). As you my or may not know, Powershell uses the .NET 3.5 runtime per default.</p>

<p>Hence, the fix was to make Powershell use the .NET 4.0 runtime, where this bug is fixed. I figured out how to do this based on <a href="http://stackoverflow.com/questions/2094694/how-can-i-run-powershell-with-the-net-4-runtime">this entry</a> on <a href="http://www.stackoverflow.com">stackoverflow</a>.
</p>

<p>I then wrote this little Powershell snippet which changes Powershell on my machine to use .NET 4.0 instead of .NET 3.5:</p>

<p><pre name="code" class="csharp">if ([Environment]::Version.Major -lt 4) {
    $configFile = Join-Path $PSHOME -ChildPath '.\powershell.exe.config'
    if (-not(Test-Path $configFile)) {
        @'
&lt;?xml version="1.0"?&gt;
&lt;configuration&gt;
  &lt;startup useLegacyV2RuntimeActivationPolicy="true"&gt;
    &lt;supportedRuntime version="v4.0.30319" /&gt;
    &lt;supportedRuntime version="v2.0.50727" /&gt;
  &lt;/startup&gt;
&lt;/configuration&gt;
'@ | Out-File -FilePath $configFile -Encoding UTF8
        "Created $configFile"
    }
    'Restart powershell in order to make it run in .NET 4'
}</pre></p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.kongsli.net%2Fnblog%2F2012%2F05%2F07%2Frunning-chocolatey-behind-an-authenticating-firewall%2F&amp;title=Running%20chocolatey%20behind%20an%20authenticating%20firewall" id="wpa2a_12"><img src="http://www.kongsli.net/nblog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.kongsli.net/nblog/2012/05/07/running-chocolatey-behind-an-authenticating-firewall/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Powershell gotchas: getting multiline string literals correct</title>
		<link>http://www.kongsli.net/nblog/2012/05/03/powershell-gotchas-getting-multiline-string-literals-correct/</link>
		<comments>http://www.kongsli.net/nblog/2012/05/03/powershell-gotchas-getting-multiline-string-literals-correct/#comments</comments>
		<pubDate>Thu, 03 May 2012 12:46:26 +0000</pubDate>
		<dc:creator>vidarkongsli</dc:creator>
				<category><![CDATA[Microsoft technologies]]></category>
		<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Software development]]></category>
		<category><![CDATA[gotcha]]></category>
		<category><![CDATA[powershell gotcha]]></category>

		<guid isPermaLink="false">http://www.kongsli.net/nblog/?p=436</guid>
		<description><![CDATA[The gotcha here is that the opening token @' or @" has to be at the end of the line, and the closing token, '@ or "@, has to be a the beginning of a line. Wrong: @'1 2 3 &#8230; <a href="http://www.kongsli.net/nblog/2012/05/03/powershell-gotchas-getting-multiline-string-literals-correct/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>The gotcha here is that the opening token <code>@'</code> or <code>@"</code> has to be at the end of the line, and the closing token, <code>'@</code> or <code>"@</code>, has to be a the beginning of a line.</p>

<p>Wrong:
<pre name="code" class="csharp">@'1
2
3
'@</pre>
<pre>Unrecognized token in source text.
At ...ps1:1 char:1
+  &lt;&lt;&lt;&lt; @'1
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : UnrecognizedToken</pre>
<pre name="code" class="csharp">@'
1
2
3'@</pre>
<pre>The string starting:
At ...:12 char:6
+ $s =  &lt;&lt;&lt;&lt; @'
is missing the terminator: '@.
At ...ps1:34 char:3
+ #> &lt;&lt;&lt;&lt;
    + CategoryInfo          : ParserError: (1
2
3'@
:String) [], ParseException
    + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString</pre>
Correct:
<pre name="code" class="csharp">@'
1
2
3
'@</pre>
<pre>1
2
3</pre></p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.kongsli.net%2Fnblog%2F2012%2F05%2F03%2Fpowershell-gotchas-getting-multiline-string-literals-correct%2F&amp;title=Powershell%20gotchas%3A%20getting%20multiline%20string%20literals%20correct" id="wpa2a_14"><img src="http://www.kongsli.net/nblog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.kongsli.net/nblog/2012/05/03/powershell-gotchas-getting-multiline-string-literals-correct/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Powershell gotchas: redirect to file encodes in Unicode</title>
		<link>http://www.kongsli.net/nblog/2012/04/20/powershell-gotchas-redirect-to-file-encodes-in-unicode/</link>
		<comments>http://www.kongsli.net/nblog/2012/04/20/powershell-gotchas-redirect-to-file-encodes-in-unicode/#comments</comments>
		<pubDate>Fri, 20 Apr 2012 09:56:13 +0000</pubDate>
		<dc:creator>vidarkongsli</dc:creator>
				<category><![CDATA[Microsoft technologies]]></category>
		<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Software development]]></category>
		<category><![CDATA[gotcha]]></category>
		<category><![CDATA[powershell gotcha]]></category>

		<guid isPermaLink="false">http://www.kongsli.net/nblog/?p=425</guid>
		<description><![CDATA[The other day, I wrote a Powershell script that would manipulate a Windows command prompt file. After some troubleshooting (note to self: RTFM) I found Powershell file redirection encodes in Unicode, more specifically UCS-2 Little Endian. The problem in this &#8230; <a href="http://www.kongsli.net/nblog/2012/04/20/powershell-gotchas-redirect-to-file-encodes-in-unicode/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>The other day, I wrote a Powershell script that would manipulate a Windows command prompt file. After some troubleshooting (note to self: RTFM) I found Powershell file redirection encodes in Unicode, more specifically UCS-2 Little Endian. The problem in this case is that <code>cmd.exe</code> does not understand Unicode. So, the follwing yields a not runnable file:
<pre name="code" class="csharp">@'
@echo off
echo Wi nøt trei a høliday in Sweden this ÿer?
'@ &gt; '.\Opening titles (unicode).cmd'
&amp; '.\Opening titles (unicode).cmd'</pre>
<pre>Output: '■@' is not recognized as an internal or external command,
operable program or batch file.</pre>
The trick is to use the <code>Out-File</code> Cmdlet instead and explicitly set the encoding. So, the following yields runnable code:
<pre name="code" class="csharp">@'
@echo off
echo Wi nøt trei a høliday in Sweden this ÿer?
'@ | Out-file '.\Opening titles (Ascii).cmd' -Encoding ASCII
&amp; '.\Opening titles (Ascii).cmd'</pre><pre>Output: Wi n?t trei a h?liday in Sweden this ?er?</pre>
In this case, it executes. However, the non-ASCII characters are not properly displayed. To achieve that as well, I used the following:
<pre name="code" class="csharp">@'
@echo off
echo Wi nøt trei a høliday in Sweden this ÿer?
'@ | Out-file '.\Opening titles (OEM).cmd' -Encoding OEM
&amp; '.\Opening titles (OEM).cmd'</pre><pre>Output: Wi nøt trei a høliday in Sweden this ÿer?</pre></p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.kongsli.net%2Fnblog%2F2012%2F04%2F20%2Fpowershell-gotchas-redirect-to-file-encodes-in-unicode%2F&amp;title=Powershell%20gotchas%3A%20redirect%20to%20file%20encodes%20in%20Unicode" id="wpa2a_16"><img src="http://www.kongsli.net/nblog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.kongsli.net/nblog/2012/04/20/powershell-gotchas-redirect-to-file-encodes-in-unicode/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Powershell: another alternative to ternary operator</title>
		<link>http://www.kongsli.net/nblog/2012/02/23/powershell-another-alternative-to-ternary-operator/</link>
		<comments>http://www.kongsli.net/nblog/2012/02/23/powershell-another-alternative-to-ternary-operator/#comments</comments>
		<pubDate>Thu, 23 Feb 2012 13:13:05 +0000</pubDate>
		<dc:creator>vidarkongsli</dc:creator>
				<category><![CDATA[Microsoft technologies]]></category>
		<category><![CDATA[Powershell]]></category>
		<category><![CDATA[powershell]]></category>
		<category><![CDATA[scripting]]></category>

		<guid isPermaLink="false">http://www.kongsli.net/nblog/?p=401</guid>
		<description><![CDATA[In C# (as well as in Java and C++), you can use the ternary operator (?:) for a shorthand notation for conditionally assigning a value: var index = (a == "a") ? 1 : 2; In Powershell, such an operator &#8230; <a href="http://www.kongsli.net/nblog/2012/02/23/powershell-another-alternative-to-ternary-operator/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>
In C# (as well as in Java and C++), you can use the <a href="http://msdn.microsoft.com/en-us/library/ty67wk28%28v=vs.80%29.aspx">ternary operator (?:)</a> for a shorthand notation for conditionally assigning a value:
</p>

<p><pre class="csharp" name="code">
var index = (a == "a") ? 1 : 2;
</pre></p>

<p>
In Powershell, such an operator is (to my knowledge) not available. One solution is described <a href="http://blogs.msdn.com/b/powershell/archive/2006/12/29/dyi-ternary-operator.aspx">here</a>. As an alternative without having to create a function and alias, I suggest:
</p>

<p><pre class="csharp" name="code">
$index = @{$true=1;$false=2}[$a -eq 'a']
</pre></p>

<p>Mmmmm, one-liner FTW <img src='http://www.kongsli.net/nblog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.kongsli.net%2Fnblog%2F2012%2F02%2F23%2Fpowershell-another-alternative-to-ternary-operator%2F&amp;title=Powershell%3A%20another%20alternative%20to%20ternary%20operator" id="wpa2a_18"><img src="http://www.kongsli.net/nblog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.kongsli.net/nblog/2012/02/23/powershell-another-alternative-to-ternary-operator/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>ASCII vs ISO-8859-1</title>
		<link>http://www.kongsli.net/nblog/2012/01/27/ascii-vs-iso-8859-1/</link>
		<comments>http://www.kongsli.net/nblog/2012/01/27/ascii-vs-iso-8859-1/#comments</comments>
		<pubDate>Fri, 27 Jan 2012 07:36:55 +0000</pubDate>
		<dc:creator>vidarkongsli</dc:creator>
				<category><![CDATA[Microsoft technologies]]></category>
		<category><![CDATA[Software development]]></category>

		<guid isPermaLink="false">http://www.kongsli.net/nblog/?p=389</guid>
		<description><![CDATA[Note to self: var ascii7BitEncoding = Encoding.ASCII; // ASCII var ascii8BitEncodingIsh = Encoding.GetEncoding("iso-8859-1"); // Extended ASCII for almost all practical purposes Where Extended ASCII differs from ISO-8859-*, according to Wikipedia: One notable way in which ISO character sets differ from &#8230; <a href="http://www.kongsli.net/nblog/2012/01/27/ascii-vs-iso-8859-1/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p><strong>Note to self:</strong></p>

<p><pre class="csharp" name="code">
var ascii7BitEncoding = Encoding.ASCII; // ASCII
var ascii8BitEncodingIsh = Encoding.GetEncoding("iso-8859-1"); // Extended ASCII for almost all practical purposes
</pre></p>

<p>Where Extended ASCII differs from ISO-8859-*, according to <a title="Wikipedia:Extended ASCII" href="http://en.wikipedia.org/wiki/Extended_ASCII">Wikipedia</a>:</p>

<p><em>One notable way in which ISO character sets differ from code pages is that the character positions 128 to 159, corresponding to ASCII control characters with the high-order bit set, are specifically unused and undefined in the ISO standards, though they had often been used for printable characters in proprietary code pages, a breaking of ISO standards that was almost universal.</em></p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.kongsli.net%2Fnblog%2F2012%2F01%2F27%2Fascii-vs-iso-8859-1%2F&amp;title=ASCII%20vs%20ISO-8859-1" id="wpa2a_20"><img src="http://www.kongsli.net/nblog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.kongsli.net/nblog/2012/01/27/ascii-vs-iso-8859-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
