<?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>Huddled Masses &#187; PoshConsole</title>
	<atom:link href="http://huddledmasses.org/tag/poshconsole/feed/" rel="self" type="application/rss+xml" />
	<link>http://huddledmasses.org</link>
	<description>You can do more than breathe for free...</description>
	<lastBuildDate>Fri, 27 Apr 2012 05:42:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
<cloud domain='huddledmasses.org' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>How to: Invoke PowerShell and use the results from C#?</title>
		<link>http://huddledmasses.org/how-to-invoke-powershell-and-use-the-results-from-csharp/</link>
		<comments>http://huddledmasses.org/how-to-invoke-powershell-and-use-the-results-from-csharp/#comments</comments>
		<pubDate>Thu, 06 Jan 2011 06:51:07 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[APIs]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[PoshConsole]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[PowerShell Host]]></category>
		<category><![CDATA[Sample Code]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1603</guid>
		<description><![CDATA[One of the questions that comes up a lot about PowerShell lately is how to use it from C#. For the sake of a simple example, I figured it&#8217;s past time I post the sort-of &#8230; Hello World of PowerShell hosting. I&#8217;m going to present this app plus a little bit more at the next [...]]]></description>
			<content:encoded><![CDATA[	<p>One of the questions that comes up a lot about PowerShell lately is how to use it from C#.  For the sake of a simple example, I figured it&#8217;s past time I post the sort-of &#8230; Hello World of PowerShell hosting. I&#8217;m going to present this app plus a little bit more at the next <a href="http://PowerShellGroup.org/rochester.ny">Upstate NY PowerShell User Group</a>, so let&#8217;s go just a little deeper than a greeting. Let&#8217;s assume that you want to run a cmdlet or a script and get the output back to display it in your application. </p>

	<p>Of course, the simplest thing to do is just stick that stuff in a <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid.aspx">DataGrid</a>, so we&#8217;ll just whip up a simple <span class="caps">WPF</span> window. Yes, <span class="caps">WPF</span> of course. I can&#8217;t go back to Windows Forms, even for just for a simple example like this, but it&#8217;s basically the same except you have to set the <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.datagrid.aspx&#39;s">DataGrid</a> DataSource property instead of the ItemsSource &#8230; anyway, here&#8217;s the XAML:</p>

	<div class="xml code xml" style="font-family:monospace;"><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Window</span> <span style="color: #000066;">x:Class</span>=<span style="color: #ff0000;">&quot;WpfPowerShell.MainWindow&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">xmlns:x</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;300&quot;</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;700&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;DataGrid</span> <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">&quot;dataGrid1&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Window<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp;</div>

	<p>Just add a reference to System.Management.Automation (it&#8217;s in the GAC: C:\Windows\assembly\GAC_MSIL\System.Management.Automation\1.0.0.0__31bf3856ad364e35\System.Management.Automation.dll) and then in your window&#8217;s constructor (or wherever you like), put this code:</p>

	<div class="csharp code csharp" style="font-family:monospace;"><br />
<span style="color: #008080; font-style: italic;">// Note: this takes a little while ...</span><br />
var ps <span style="color: #008000;">=</span> PowerShell.<span style="color: #0000FF;">Create</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #008080; font-style: italic;">// Let's get all the processes:</span><br />
ps.<span style="color: #0000FF;">AddCommand</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Get-Process&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #008080; font-style: italic;">// Use the generic overload to cast the PSObjects to their base object:</span><br />
panel.<span style="color: #0000FF;">DataContext</span> <span style="color: #008000;">=</span> ps.<span style="color: #0000FF;">Invoke</span><span style="color: #008000;">&lt;</span>Process<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp;</div>

	<p>That&#8217;s it. We&#8217;re done.</p>

	<h3>And now for something slightly more interesting</h3>

	<p>What about if we also wanted an easy way to spot the biggest memory consumers? We could start by sorting the processes by the WorkingSet, and pick just the 15 worst offenders. But let&#8217;s also add a chart to <em>visualize</em> just how bad those top offenders are. In <span class="caps">WPF</span>, this is simple: I&#8217;m going to use the free <a href="http://wpf.amcharts.com/">amCharts</a> in my example, but you can do the same thing with the free <a href="http://visifire.com/">visifire</a> or <a href="http://www.visiblox.com">visiblox</a> charts, or the (commercial only) <a href="http://www.telerik.com/products/wpf/chart.aspx">Telerik</a> <span class="caps">RAD</span> controls &#8230; etc. All of those work in both <span class="caps">WPF</span> and Silverlight (and all three of the &#8220;free&#8221; ones have commercial versions).</p>

	<div class="xml code xml" style="font-family:monospace;"><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Window</span> <span style="color: #000066;">x:Class</span>=<span style="color: #ff0000;">&quot;WpfPowerShell.MainWindow&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">xmlns:x</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">xmlns:am</span>=<span style="color: #ff0000;">&quot;http://schemas.amcharts.com/charts/wpf/2009/xaml&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">Title</span>=<span style="color: #ff0000;">&quot;PowerShell Demo&quot;</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;400&quot;</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;800&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Grid.ColumnDefinitions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ColumnDefinition</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;3*&quot;</span> <span style="color: #000066;">MaxWidth</span>=<span style="color: #ff0000;">&quot;350&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ColumnDefinition</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;4*&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Grid.ColumnDefinitions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;am:PieChart</span> <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">&quot;pieChart1&quot;</span> <span style="color: #000066;">ValueMemberPath</span>=<span style="color: #ff0000;">&quot;WorkingSet&quot;</span> <span style="color: #000066;">TitleMemberPath</span>=<span style="color: #ff0000;">&quot;ProcessName&quot;</span> <span style="color: #000066;">SlicesSource</span>=<span style="color: #ff0000;">&quot;{Binding}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;DataGrid</span> <span style="color: #000066;">Grid.Column</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">&quot;dataGrid1&quot;</span> <span style="color: #000066;">ItemsSource</span>=<span style="color: #ff0000;">&quot;{Binding}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Window<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp;</div>

	<p>Of course, this got a little more complicated. We needed to put the controls in a panel of some sort (I chose to use a Grid mainly because it automatically causes the scroll viewer around the DataGrid, but that forces us to specify the ColumnDefinitions) and the PieChart needs bindings for the value and title of each slice. You&#8217;ll notice I set the ItemsSource of the DataGrid and the SlicesSource of the PieChart to &#8220;{Binding}&#8221; (side note: why can&#8217;t they just use &#8220;ItemsSource&#8221; like normal people?) ... </p>

	<p>Setting the sources to {Binding} causes them to just look at their data context, so now in the code behind, I can bind them <em>both at once</em> by just setting the DataContext of the window (try that in Windows Forms).</p>

	<div class="csharp code csharp" style="font-family:monospace;"><br />
<span style="color: #008080; font-style: italic;">// Note: this takes a little while ...</span><br />
var ps <span style="color: #008000;">=</span> PowerShell.<span style="color: #0000FF;">Create</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #008080; font-style: italic;">// Get the 15 biggest memory users. Note that we're using AddScript now,</span><br />
<span style="color: #008080; font-style: italic;">// We should probably put this in a resource or a separate script file:</span><br />
ps.<span style="color: #0000FF;">AddScript</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Get-Process | Sort WorkingSet | Select -Last 15&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #008080; font-style: italic;">// Use the generic overload to cast the PSObjects to their base object, </span><br />
<span style="color: #008080; font-style: italic;">// Just set the DataContext of the window, the controls are data bound</span><br />
DataContext <span style="color: #008000;">=</span> ps.<span style="color: #0000FF;">Invoke</span><span style="color: #008000;">&lt;</span>Process<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp;</div>

	<p>That&#8217;s all there is to it. I can attach my solution after I give my presentation if people want it, but there&#8217;s really nothing else in it (so far) except the references to the amCharts and System.Management.Automation &#8230;</p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/how-to-invoke-powershell-and-use-the-results-from-csharp/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PowerShell User Group and Community News!</title>
		<link>http://huddledmasses.org/powershell-user-group-and-community-news/</link>
		<comments>http://huddledmasses.org/powershell-user-group-and-community-news/#comments</comments>
		<pubDate>Tue, 11 Nov 2008 18:06:40 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Automation]]></category>
		<category><![CDATA[PoshCode]]></category>
		<category><![CDATA[PoshConsole]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[User Group]]></category>
		<category><![CDATA[WASP]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/powershell-user-group-and-community-news/</guid>
		<description><![CDATA[I keep forgetting to mention this! I&#8217;m speaking at the next PowerShell Virtual User Group this Thursday, November 13th at 12PM EST. Join us if you&#8217;re interested in any of my PowerShell CodePlex projects (PoshConsole, PoshCode, WASP). Actually, join us even if you&#8217;re not, because there&#8217;s going to be a demo of the latest version [...]]]></description>
			<content:encoded><![CDATA[	<p>I keep forgetting to mention this!  I&#8217;m speaking at the <a href="http://marcoshaw.blogspot.com/2008/10/windows-powershell-virtual-user-group.html">next PowerShell Virtual User Group</a> this Thursday, November 13th at 12PM <span class="caps">EST</span>. <a href="https://www.livemeeting.com/cc/winserver_usergroup/join?id=4RS44G&#38;role=attend&#38;pw=8%5E-%5CzqX">Join us</a> if you&#8217;re interested in any of my PowerShell CodePlex projects (<a href="http://CodePlex.com/PoshConsole">PoshConsole</a>, <a href="http://CodePlex.com/PoshCode">PoshCode</a>, <a href="http://CodePlex.com/WASP">WASP</a>). Actually, join us even if you&#8217;re not, because there&#8217;s going to be a demo of the latest version of PowerShell Plus by the folks from Idera, and Nathan Winters will be discussing managing Exchange with PowerShell.</p>

	<p>Also, the <a href="http://PowerShellCommunity.com">PowerShell Community</a> is running <a href="http://spreadsheets.google.com/viewform?key=pK60fL5qBA8pe-_giq0ipXw">a survey</a> about the possibility of a PowerShell convention, and what you&#8217;re interested in seeing at one&#8230; it&#8217;ll just take a moment  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=':)' class='wp-smiley' /> </p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/powershell-user-group-and-community-news/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Is PowerShell $ShellId too big a burden?</title>
		<link>http://huddledmasses.org/is-powershell-shellid-too-big-a-burden/</link>
		<comments>http://huddledmasses.org/is-powershell-shellid-too-big-a-burden/#comments</comments>
		<pubDate>Sun, 14 Sep 2008 22:26:42 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Bugs]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[PoshConsole]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[RunspaceConfiguration]]></category>
		<category><![CDATA[ShellId]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/?p=655</guid>
		<description><![CDATA[As you may know, I was one of the first developers who jumped on board and started working on an alternative PowerShell host (actually, I&#8217;m also the first to create a WPF-based host, and the first to create one that was open source &#8230; but enough about me). Recently I&#8217;ve picked back up on that [...]]]></description>
			<content:encoded><![CDATA[	<p>As you may know, I was one of the first developers who jumped on board and started working on an alternative PowerShell host  (actually, I&#8217;m also the first to create a WPF-based host, and the first to create one that was open source &#8230; but enough about me).</p>

	<p>Recently I&#8217;ve picked back up on that project, and am just about ready to release what I hope will be the last &#8220;pre-release&#8221; of PoshConsole before I declare it to be &#8220;beta&#8221; quality and start doing more regular releases.  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=';-)' class='wp-smiley' /> </p>

	<p><a href="http://HuddledMasses.org/wordpress/wp-content/uploads/2008/09/thumb_get-weather.png" class="float-left-block"><img src="http://HuddledMasses.org/wordpress/wp-content/uploads/2008/09/thumb_get-weather.png" alt="Trivial example of inline WPF output" title="Get-Weather, in WPF" width="363" height="180" class="size-full wp-image-656" /></a>The coolest features of PoshConsole, the ones that are really revolutionary, involve exposing the <span class="caps">WPF</span> surface to scripts and cmdlets so that you can actually have graphical output <strong>in</strong> the console &#8212; not popups, and not just for fun&#8230; but stuff like putting bar graphs behind the size columns in Get-ChildItem for folders, and the memory columns in Get-Process, etc&#8230; anyway.</p>

	<p>So I&#8217;ve been writing a few scripts to show off the possibilities of PoshConsole, and was thinking about even posting them on the <a href="http://PoshCode.org">PoshCode.org</a> <a href="http://PoshCode.org">PowerShell Script Repository</a>, but I wanted a way to make clear that they&#8217;d only work in PoshConsole.  </p>

	<p>A little investigation later, and it was clear that PowerShell has a built in feature for this: <code>#requires -ShellId PoshConsole</code> &#8230; except, it doesn&#8217;t work. Actually, it doesn&#8217;t work for two reasons:</p>

	<ol>
		<li>PowerShell always ignores <code>#requires -ShellId Anything</code></li>
	</ol>
	<ol>
		<li>No other PowerShell host implements ShellId as far as I can see</li>
	</ol>

	<p>The ShellId is a read-only property of the RunspaceConfiguration, so to implement it in your PowerShell host you have to create your own RunspaceConfiguration class inheriting from the abstract base class.  The problem is, there are some expensive side-effects:</p>

	<ol>
		<li>You have to configure the available .net Assemblies, Cmdlets, Format files, Initialization Scripts, Providers, Scripts, and Type files &#8230;</li>
		<li>Having your own ShellId means you don&#8217;t inherit anything from PowerShell.exe (like for instance, the <a href="http://www.windowsecurity.com/articles/PowerShell-Security.html">ExecutionPolicy</a>)</li>
	</ol>
	<ol>
		<li>Some cmdlets just don&#8217;t seem to work (Add-PSSnapin is just failing on me with &#8220;Object reference not set to an instance of an object&#8221;)</li>
	</ol>

	<p>So &#8230; to sum up: PowerShell ignores #require -ShellId, and I can&#8217;t find another host (including Microsoft&#8217;s &#8220;Graphical PowerShell&#8221; and the latest and greatest PowerShell Plus Professional) that bothers to set the ShellId. Can anyone tell me a reason why I should bother with this?</p>

	<p>Oh, and, can anyone give me some information about why Add-PSSnapin might be failing when I <strong>do</strong> set the ShellId?</p>

	<h2> <img src='http://huddledmasses.org/wordpress/wp-includes/' alt='[new]' class='wp-smiley' />  ShellId <strong>IS</strong> too big a burden.</h2>

	<p>After further investigation, it turns out that the reason Add-PSSnapin fails is because it uses a method <code>AddPsSnapIn</code> which is part of the RunspaceConfiguration (that you had to create yourself to set the ShellId), and you can&#8217;t implement that method  (in fact, you can&#8217;t seem to override it with &#8220;new&#8221; either, I&#8217;ll have to look into that a little more).</p>

	<p>In any case, the method has to return a PSSnapInInfo object, and since there are no public constructors for PSSnapinInfo objects, you&#8217;re sore-out-of-luck.  It appears you would literally have to reverse engineer the whole &#8220;PSSnapin&#8221; system and create your own cmdlets and functionality around it to try to keep your host compatible with the main PowerShell.  No wonder nobody&#8217;s done this&#8230;</p>

	<p>I guess I&#8217;ll just go back to using Microsoft.PowerShell as my ShellId, I don&#8217;t know what I was thinking.</p>

]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/is-powershell-shellid-too-big-a-burden/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>XamlReader and Resource Library fonts in WPF</title>
		<link>http://huddledmasses.org/xamlreader-and-resource-library-fonts-in-wpf/</link>
		<comments>http://huddledmasses.org/xamlreader-and-resource-library-fonts-in-wpf/#comments</comments>
		<pubDate>Thu, 30 Aug 2007 05:15:16 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[My Software]]></category>
		<category><![CDATA[PoshConsole]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/jaykul/xamlreader-and-resource-library-fonts-in-wpf/</guid>
		<description><![CDATA[Microsoft has provided some very good documentation on how to package fonts with WPF applications, and I&#8217;ve been following it in several different ways in different apps I&#8217;ve written. Recently I provided a feature in Posh Console which allows you to load the startup banner from an external file: StartupBanner.xaml. Loading the banner from an [...]]]></description>
			<content:encoded><![CDATA[	<p>Microsoft has provided some <a href="http://msdn2.microsoft.com/en-us/library/ms753303.aspx">very good documentation</a> on how to package fonts with <span class="caps">WPF</span> applications, and I&#8217;ve been following it in several different ways in different apps I&#8217;ve written.  Recently I provided a feature in <a href="http://CodePlex.com/PoshConsole">Posh Console</a> which allows you to  load the startup banner from an external file: StartupBanner.xaml.  </p>

	<p>Loading the banner from an external <span class="caps">XAML</span> file means that users can configure their own startup banner and make it look however they like, but it broke our default banner.  The startup banner we&#8217;ve been using has a logo and some text which are all defined in pure <span class="caps">XAML</span> and uses several fonts which we had embedded in a FontLibrary.dll resource assembly. It worked great as long as the <span class="caps">XAML</span> was defined in the application, but as soon as we removed it to an external file and loaded it in using the <a href="http://msdn2.microsoft.com/en-us/library/ms590398.aspx" title=" XmlReader ">XamlReader.Load</a> method, the fonts all went to the fallback default fonts instead of our embedded ones. </p>

	<p>Normally this might not be a big deal, but since our logo is based on the &#8220;Q&#8221; in a Quake-like font &#8230; it looks really lame without fonts.  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=':D' class='wp-smiley' />  Apparently, when you load external <span class="caps">XAML</span>, it can&#8217;t use the <a href="http://msdn2.microsoft.com/en-us/library/ms753303.aspx#creating_a_font_resource_library">Font Resource Library</a> resources.  At this point, I can&#8217;t seem to find anything to indicate why this is the case, but I suspect it has to do with the fact that the external, loaded <span class="caps">XAML</span> <a href="http://msdn2.microsoft.com/en-us/library/aa970910.aspx">only has partial trust</a>, and since the font is in an external assembly instead of embedded in the partial trust content, it can&#8217;t get to it.  However, it might be simpler than that: it might just be that the base <span class="caps">URI</span> for the externally loaded <span class="caps">XAML</span> is an actual file location rather than one of the <code>pack://,,,application/</code> type URIs, so the slightly odd font path doesn&#8217;t resolve.</p>

	<p>In any case, I found a way around it, which is of course, why I&#8217;m writing all of this down.  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=':)' class='wp-smiley' />  <span id="more-428"></span> If you declare <span class="caps">WPF</span> <em>resources</em> in your project, you can inject them into the <span class="caps">WPF</span> objects loaded from the <span class="caps">XAML</span> to make them available to them. So for instance, I have a font called &#8220;Quake&#8221; in the FontLibrary assembly.  I can declare it as a resource in my application&#8217;s <span class="caps">XAML</span> (at the app level, window level, or wherever).</p>

	<div class="xml code xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Style</span> <span style="color: #000066;">x:Key</span>=<span style="color: #ff0000;">&quot;POSHFont&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Setter</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;TextElement.FontFamily&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;/FontLibrary;Component/#QUAKE&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Style<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div>

	<p>The font in that style gets resolved in full trust with the <em>pack</em> <span class="caps">URI</span>, and it works fine when used from my application. Having created that, I can do the same thing in the external <span class="caps">XAML</span> files, and apply the style to the text as a <a href="http://msdn2.microsoft.com/en-us/library/ms748942.aspx">DynamicResource</a> &#8230;
 and even feel free to override parts of the style if I want to.  For instance, even though the style might specify a FontSize, if I refer to the style on this text block and specify my own FontSize, the one that&#8217;s on the TextBlock (or Paragraph, or Run, or whatever) will be used.  Now, we&#8217;re not quite done yet, because although they look the same in source, the fonts won&#8217;t resolve properly in the externally loaded <span class="caps">XAML</span>.</p>

	<div class="xml code xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBlock</span> <span style="color: #000066;">TextAlignment</span>=<span style="color: #ff0000;">&quot;Center&quot;</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;180&quot;</span> <span style="color: #000066;">Style</span>=<span style="color: #ff0000;">&quot;{DynamicResource POSHFont}&quot;</span> <span style="color: #000066;">FontSize</span>=<span style="color: #ff0000;">&quot;145&quot;</span> <span style="color: #000066;">Foreground</span>=<span style="color: #ff0000;">&quot;#FF3275B9&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBlock.BitmapEffect<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;BevelBitmapEffect</span> <span style="color: #000066;">BevelWidth</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">EdgeProfile</span>=<span style="color: #ff0000;">&quot;CurvedOut&quot;</span> <span style="color: #000066;">LightAngle</span>=<span style="color: #ff0000;">&quot;258&quot;</span> <span style="color: #000066;">Relief</span>=<span style="color: #ff0000;">&quot;0.03&quot;</span> <span style="color: #000066;">Smoothness</span>=<span style="color: #ff0000;">&quot;0.565&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/TextBlock.BitmapEffect<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Q<br />
&nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/TextBlock<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div>

	<p>However, because the style is specified as a dynamic resource rather than a static one, I don&#8217;t have to define that resource at all in that <span class="caps">XAML</span> file, but even if I do, when I load the <span class="caps">XAML</span> into my application, I can simply set it&#8217;s resources from the resources that I&#8217;ve loaded in full trust and with the <em>pack://</em> base <span class="caps">URI</span>...  In my case, I have actually defined several fonts as resources on a Document element, so I just copy them all into the element I read in from the <span class="caps">XAML</span> file:</p>

	<div class="csharp code csharp" style="font-family:monospace;">Paragraph banner <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>Paragraph<span style="color: #000000;">&#41;</span>XamlReader.<span style="color: #0000FF;">Load</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">System.<span style="color: #0000FF;">Xml</span></span>.<span style="color: #0000FF;">XmlReader</span>.<span style="color: #0000FF;">Create</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;StartupBanner.xaml&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>banner <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; <span style="color: #008080; font-style: italic;">// Copy over resources from my DOCUMENT to the BANNER</span><br />
&nbsp; <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> key <span style="color: #0600FF;">in</span> Document.<span style="color: #0000FF;">Resources</span>.<span style="color: #0000FF;">Keys</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; banner.<span style="color: #0000FF;">Resources</span><span style="color: #000000;">&#91;</span>key<span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> Document.<span style="color: #0000FF;">Resources</span><span style="color: #000000;">&#91;</span>key<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span> <br />
&nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; Document.<span style="color: #0000FF;">Blocks</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>_currentParagraph<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #000000;">&#125;</span></div>

	<p>And that&#8217;s it: <a href="/images/PoshConsole/StartupBanner.png"><img src="/images/PoshConsole/StartupBannerLogo.png" width="182" height="182" alt="PoshConsole Banner" style="padding: 4px; text-align:center; vertical-align:top" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/xamlreader-and-resource-library-fonts-in-wpf/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>PowerShell Host and Custom Formatting</title>
		<link>http://huddledmasses.org/powershell-host-and-custom-formatting/</link>
		<comments>http://huddledmasses.org/powershell-host-and-custom-formatting/#comments</comments>
		<pubDate>Thu, 30 Aug 2007 00:01:09 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[My Software]]></category>
		<category><![CDATA[PoshConsole]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Scripting]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/jaykul/powershell-host-and-custom-formatting/</guid>
		<description><![CDATA[So, I&#8217;ve been working on PoshConsole for awhile now, and with the help of some of the guys in #PowerShell@irc.freenode.net have been playing with trying to find ways to enhance output, like using ANSI escape sequences, and even creating an Out-WPF cmdlet which can output objects as databound WPF controls which look really good. We [...]]]></description>
			<content:encoded><![CDATA[	<p>So, I&#8217;ve been working on PoshConsole for awhile now, and with the help of some of the guys in #PowerShell@irc.freenode.net have been playing with trying to find ways to enhance output, like using <a href="http://poshconsole.org/images/PoshConsole/AnsiEscapeSequences.jpg"><span class="caps">ANSI</span> escape sequences</a>, and even creating an <a href="http://poshconsole.org/images/PoshConsole/Out-WPF2.png">Out-<span class="caps">WPF</span> cmdlet</a> which can output objects as databound <span class="caps">WPF</span> controls which look really good.</p>

	<p>We have been trying to find a way to incorporate the colored and even bold/italic formatting in a way that would be compatible with the existing format-* cmdlets, and we&#8217;ve even looked at replacing out-default with a cmdlet that would be compatible with the default PowerShell host.  But today I finally locked in on <a href="http://blogs.msdn.com/powershell/archive/2006/04/25/583269.aspx">the import of this post on the PowerShell blog.</a>  It&#8217;s just not possible.</p>

	<p>The format cmdlets (Format-List, Format-Custom, Format-Table, Format-Wide) output data in the form of undocumented .Net objects which are &#8220;subject to change without notice,&#8221; and are therefore basically useless.  Of course, that means that if you want to replace Out-Default you have to not only replace the output, but you have to replace the formatting cmdlets, and of course the Update-FormatData cmdlet too &#8212; in fact, you have to <strong>either</strong> parse the largely undocumented format data files, or create some replacement for them to allow users to specify formatting for types you haven&#8217;t thought of&#8230; </p>

	<p>All of this amounts to what many of you probably already knew: it&#8217;s a lot of work to create a complete PowerShell host, but it&#8217;s at least as much work to create a replacement PowerShell formatter.</p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/powershell-host-and-custom-formatting/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A quick progress update on PoshConsole</title>
		<link>http://huddledmasses.org/a-quick-progress-update-on-poshconsole/</link>
		<comments>http://huddledmasses.org/a-quick-progress-update-on-poshconsole/#comments</comments>
		<pubDate>Tue, 21 Aug 2007 05:10:43 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[DeskOps]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[PoshConsole]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[UserInterface]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/jaykul/a-quick-progress-update-on-poshconsole/</guid>
		<description><![CDATA[Click for a teaser screenshot This is actually a bit scary, because you can load sort-of &#8230; anything To prove it (you&#8217;re gonna love this &#8230; I should take a video, really, but it&#8217;s time for bed, so this will have to do): check this out Beat that! So yeah, I have this control written [...]]]></description>
			<content:encoded><![CDATA[	<p><a href="http://poshconsole.org/images/PoshConsole/Out-WPF.png">Click for a teaser screenshot</a></p>

	<p>This is actually a bit scary, because you can load sort-of &#8230; anything  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=';-)' class='wp-smiley' />  To prove it (you&#8217;re gonna love this &#8230; I should take a video, really, but it&#8217;s time for bed, so this will have to do): <a href="http://poshconsole.org/images/PoshConsole/Out-WPF2.png">check this out</a> Beat that!  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=';)' class='wp-smiley' /> </p>

	<p>So yeah, I have this control written from an earlier project which creates a task list with live preview images. When I say &#8220;live previews&#8221; I mean actually live, like on the Vista Alt+Tab: if there&#8217;s a video playing, you can watch it in there (of course, it dies a horrible death on XP, but I have other code for that).  </p>

	<p>As a side note  the TaskBar2.panel stuff is from the <span class="caps">TINS</span> release I made months ago, there&#8217;s several fun .panel files in there &#8230; but you have to load a bunch of the <span class="caps">TINS</span> assemblies first, I didn’t distinguish which ones, I just did:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #660033;">cd</span> C:\Users\Joel\Projects\TechDemos\TINS\bin\Release<br />
<span style="color: #660033;">ls</span> <span style="color: #66cc66;">*</span>.<span style="color: #003366;">dll</span>,<span style="color: #66cc66;">*</span>.<span style="color: #003366;">exe</span> <span style="color: #66cc66;">|</span> <span style="color: #66cc66;">%</span> <span style="color: #333;">&#123;</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>System.<span style="color: #003366;">Reflection</span>.<span style="color: #003366;">Assembly</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">LoadFrom</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$_</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span><br />
<span style="color: #660033; font-weight: bold;">$Host</span>.<span style="color: #003366;">PrivateData</span>.<span style="color: #003366;">XamlUI</span>.<span style="color: #003366;">LoadXaml</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;C:\Users\Joel\Projects\TechDemos\TINS\bin\Release\Panels\TaskBar2.panel&quot;</span><span style="color: #333;">&#41;</span>;<br />
&nbsp;</div>

	<p>Of course, in .Net 3.5 the RichTextBox has the ability to go <code>ContentEnabled=&#34;true&#34;</code> which should allow those things to be actual controls you can click to switch active task, right-click to get a task menu, etc (although I haven&#8217;t tried that yet).  We might just have to upgrade  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=';-)' class='wp-smiley' /> </p>

	<p>Anyway, it&#8217;s past time for bed, so I&#8217;ll preempt complaints about how this isn&#8217;t ready for release yet with this quote: </p>

	<blockquote>
		<p>I don’t have to take this abuse from you; I’ve got hundreds of people waiting to abuse me. &#8212;  Bill Murray, “Ghostbusters”</p>
	</blockquote>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/a-quick-progress-update-on-poshconsole/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PoshConsole &#8211; a Smart, Fashionable PowerShell Console</title>
		<link>http://huddledmasses.org/poshconsole-a-smart-fashionable-powershell-console/</link>
		<comments>http://huddledmasses.org/poshconsole-a-smart-fashionable-powershell-console/#comments</comments>
		<pubDate>Thu, 05 Jul 2007 14:31:14 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[My Software]]></category>
		<category><![CDATA[PoshConsole]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/jaykul/poshconsole-a-smart-fashionable-powershell-console/</guid>
		<description><![CDATA[So, I&#8217;ve been practically invisible for a few weeks here, and liable to be invisible for at least one more week &#8230; this is my vacation time, so it&#8217;s hard to motivate myself to blog about anything. However, I have been spending some time working on a side project: a PowerShell Host. Now, there are [...]]]></description>
			<content:encoded><![CDATA[	<p>So, I&#8217;ve been practically invisible for a few weeks here, and liable to be invisible for at least one more week &#8230; this is my vacation time, so it&#8217;s hard to motivate myself to blog about anything.  However, I have been spending some time working on a side project: a PowerShell Host.</p>

	<p>Now, there are a few other hosts out there, but with <a href="http://www.powershellanalyzer.com/">PowerShell Analyzer</a> seemingly swallowing up <a href="http://powershell.com/">PowerShell IDE</a> and going the <a href="http://www.powershell.com/store/">fifty dollar route</a> &#8230; as an open sourcer and contributor to the <a href="http://CodePlex.com/PowerShellCX">PowerShell Community Extensions</a> it seemed to me that this couldn&#8217;t be that hard, so I started looking for an open source implementation of PSHostRawUserInterface and couldn&#8217;t find one.  I mean, there are a couple of implementations out there, but essentially they are the PowerShell Remoting interface (which actually runs inside PowerShell.exe, and thus doesn&#8217;t seem to implement anything itself) and a half-implemented console block from a Japanese file manager called <a href="http://cvs.sourceforge.jp/cgi-bin/viewcvs.cgi/kuon/kuon/">kuon</a> &#8230; </p>

	<p>So, being myself, I stepped in an implemented it in the weirdest way I could think of: on top of a <span class="caps">WPF</span> RichTextBox.  Yep, that&#8217;s right.  I&#8217;ve made myself a ConsoleTextBox which subclasses the RichTextBox and handles keyboard input etc to behave &#8230; roughly &#8230; the way a Console should.</p>

	<p>It&#8217;s <a href="http://www.codeplex.com/PoshConsole/WorkItem/List.aspx">far from finished</a>, but it <strong>is</strong> working, so this is by way of announcing the first public alpha release of <a href="http://www.codeplex.com/PoshConsole/">PoshConsole</a>! The vast majority of PowerShell scripts will run without a hitch &#8212; the only time you&#8217;ll have problems is with secure string input (eg: passwords), with direct raw buffer interaction, and with running &#8220;legacy&#8221; console applications &#8212; it does not yet redirect standard input/output/error.</p>

	<p>One small tip: if you have the PowerShell Community Extensions installed, you&#8217;ll want to remove the alias they set up to pipe help through &#8220;less&#8221; ... because less is a legacy console app &#8212; you won&#8217;t get any help at all.</p>

	<p>Technorati Tags: <a class="performancingtags" href="http://technorati.com/tag/PowerShell" rel="tag">PowerShell</a>, <a class="performancingtags" href="http://technorati.com/tag/Scripting" rel="tag">Scripting</a>, <a class="performancingtags" href="http://technorati.com/tag/Windows" rel="tag">Windows</a>, <a class="performancingtags" href="http://technorati.com/tag/Command Line" rel="tag">Command Line</a></p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/poshconsole-a-smart-fashionable-powershell-console/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

