<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
  version="2.0">
  <channel>
    <title>Objects, Services, and the rants of a lunatic operating out of Microsoft</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/schlepticons/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/schlepticons/feed.xml</link>
    <description>Have coffee, will travel...</description>
    <lastBuildDate>Wed, 17 Apr 2019 03:24:45 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>Free Code: Deferred calls to IDispose</title>
      <link>https://docs.microsoft.com/archive/blogs/schlepticons/free-code-deferred-calls-to-idispose</link>
      <pubDate>Wed, 18 Jun 2014 12:50:00 GMT</pubDate>
      <dc:creator><![CDATA[Jimmy Zimms]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/schlepticons/2014/06/18/free-code-deferred-calls-to-idispose/</guid>
      <description><![CDATA[I've been dealing with heavy use of Stream types recently. Not as in the fairly common usages of...]]></description>
      <content:encoded><![CDATA[<p>I've been dealing with heavy use of Stream types recently. Not as in the fairly common usages of say, reading a text file or whatnot. We're talking Streams that transform data from one format to the other transparently to the caller, that run across various nodes in a cluster (think a Stream of Streams), that sort of deal. So I found it a pleasant situation where I actually needed to perform some simple good-enough alerting of some operations team when some email messages needed to be intercepted by our system and the set of attachments actually resent along in another email. Nothing too crazy here. However if anyone has ever used the MailAttachment class before, they should know that the overload that accepts another stream doesn't actually read it until you send the email message itself. Not so much a problem except when you like to clean up behind yourself but what you are doing is operating on a tight unbounded loop and the references to the Stream instances are highly ephemeral. That Stream might be expensive after all. What I really needed to do was dispose of them at a defined point in the future but without cluttering up the the admittedly simple code I need to accomplish with cruft about issues orthogonal to what I am doing. So I whipped out this simple component that can be scoped to a region of code that you can rely on to clean up once you exit the block.</p>
<p>Here's an example of mis-use that looks correct and how the code should seemingly work:</p>
<p>var anEmail = new MailMessage(yadaYadaYada); // settings cruft omitted for brevity<br />foreach (var thingToEmail in GetThingsToEmail())<br />{<br />&nbsp; using (var stream = thingToEmail.OpenStream())<br />&nbsp; {<br />&nbsp; &nbsp; var attachment = new Attachement(stream);<br />&nbsp; &nbsp; anEmail.Attachments.Add(attachment);<br />&nbsp; }<br />}<br />anSmtpClient.Send(anEmail); // BOOM-Streams are closed so exception is thrown</p>
<p>Well that sucks, right? We could write some ugly try..finally code and keep a bag of the streams and then iterate them in the finally and call dispose<br />OR<br />we can keep the simple pattern above and still work with the API limitations with this handy little component:<br /><br />var anEmail = new MailMessage(yadaYadaYada); // settings cruft omitted for brevity<br />using (var scope = new AggregateDisposable())<br />{<br />&nbsp; foreach (var thingToEmail in GetThingsToEmail())<br />&nbsp; {<br />&nbsp; &nbsp; var stream = thingToEmail.OpenStream();<br />&nbsp; &nbsp; scope.Track(stream);<br />&nbsp; &nbsp; var attachment = new Attachement(stream);<br />&nbsp; &nbsp; anEmail.Attachments.Add(attachment);<br />&nbsp; &nbsp;}<br />&nbsp; anSmtpClient.Send(anEmail); // Happy happy Joy joy<br />}</p>
<p>And Bob's your uncle. Just because I happen to be a very nice guy I have provided a nice overload that actually allows you to materialize the IDisposable component, track it, and set it to your choice of variable in one go like this=&gt;<br />var stream = scope.Track(() =&gt;&nbsp;thingToEmail.OpenStream());</p>
<p>Well there you go. Enjoy!</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/10/53/54/77/AggregateDisposable.zip" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-10-53-54-77/AggregateDisposable.zip">AggregateDisposable.zip</a></p>]]></content:encoded>
    </item>
    <item>
      <title>IIS Kerberos Authentication Checklist</title>
      <link>https://docs.microsoft.com/archive/blogs/schlepticons/iis-kerberos-authentication-checklist</link>
      <pubDate>Mon, 19 May 2014 12:38:00 GMT</pubDate>
      <dc:creator><![CDATA[Jimmy Zimms]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/schlepticons/2014/05/19/iis-kerberos-authentication-checklist/</guid>
      <description><![CDATA[Just cross posting so I don't lose track of...]]></description>
      <content:encoded><![CDATA[<p>Just cross posting so I don't lose track of this.</p>
<p>&nbsp;</p>
<p><a href="http://blogs.msdn.com/b/webtopics/archive/2009/01/19/service-principal-name-spn-checklist-for-kerberos-authentication-with-iis-7-0.aspx">http://blogs.msdn.com/b/webtopics/archive/2009/01/19/service-principal-name-spn-checklist-for-kerberos-authentication-with-iis-7-0.aspx</a></p>]]></content:encoded>
    </item>
    <item>
      <title>Free Code: Windsor Castle Autofactory Facility</title>
      <link>https://docs.microsoft.com/archive/blogs/schlepticons/free-code-windsor-castle-autofactory-facility</link>
      <pubDate>Fri, 01 Mar 2013 12:00:00 GMT</pubDate>
      <dc:creator><![CDATA[Jimmy Zimms]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/schlepticons/2013/03/01/free-code-windsor-castle-autofactory-facility/</guid>
      <description><![CDATA[So I recently was working on a system that used extensive need of delayed factory support...]]></description>
      <content:encoded><![CDATA[<p>So I recently was working on a system that used extensive need of delayed factory support for&nbsp;dependencies&nbsp;on our components. Effectively if ClassDepending needed to use ClassDepended, the constructor parameter was instead Func&lt;ClassDepended&gt;. This solution was built on <a href="http://www.castleproject.org/">Windsor</a>&nbsp;container. Having to remember to register all those custom factories in our bootstrap routines was becoming a huge Pain In The Tuchus so I knocked out a quick and dirty facility (one of the core&nbsp;extensibility&nbsp;points for Winsdor) that will monitor registrations and will build a complementary factory delegate registration for the same component <em>automagically</em>! By default it will take the registration key of the core component and register a singleton instance of the Func&lt;YourComponent&gt; under the key <em>originalkey_factory</em>. And before you ask YES it is smart enough to not register factories for factories. Basically anything that is a delegate type is ignored. Try her out and see how you like her.</p>
<p>&nbsp;</p>
<p>Basic Usage:</p>
<p><span style="font-family: 'andale mono', times;"><span style="color: #0000ff;">var</span> myContainer = <span style="color: #0000ff;">new</span> <span style="color: #008080;">WindsorContainer</span>();</span><br /><span style="font-family: 'andale mono', times;">myContainer.Facilities&lt;<span style="color: #008080;">AutoFactoryFacility</span>&gt;();</span></p>
<p><span style="font-family: 'andale mono', times;">// initialize your container registerations</span></p>
<p><span style="font-family: 'andale mono', times;"><span style="color: #0000ff;">var</span> foo = myContainer.Resolve&lt;<span style="color: #008080;">IFoo</span>&gt;();</span><br /><span style="font-family: 'andale mono', times;"><span style="color: #008080;">Debug</span>.Assert(foo != null);</span><br /><br /><span style="font-family: 'andale mono', times;"><span style="color: #0000ff;">var</span> fooFactory = myContainer.Resolve&lt;<span style="color: #008080;">Func</span>&lt;<span style="color: #008080;">IFoo</span>&gt;&gt;();</span><br /><span style="font-family: 'andale mono', times;"><span style="color: #008080;">Debug</span>.Assert(fooFactory != null);</span></p>
<p><span style="font-family: 'andale mono', times;"><span style="color: #0000ff;">var</span> foo 2 = fooFactory();</span><br /><span style="font-family: 'andale mono', times;"><span style="color: #008080;">Debug</span>.Assert(foo2 != null);</span>&nbsp;</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/10/39/86/27/AutoFactoryFacility.cs" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-10-39-86-27/AutoFactoryFacility.cs">AutoFactoryFacility.cs</a></p>]]></content:encoded>
    </item>
    <item>
      <title>Free Code: WriteOnlySet&lt;T&gt;</title>
      <link>https://docs.microsoft.com/archive/blogs/schlepticons/free-code-writeonlysett</link>
      <pubDate>Thu, 28 Feb 2013 14:18:00 GMT</pubDate>
      <dc:creator><![CDATA[Jimmy Zimms]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/schlepticons/2013/02/28/free-code-writeonlysett/</guid>
      <description><![CDATA[So had a fun business entity that needed a write only set of data that contained no duplicates...]]></description>
      <content:encoded><![CDATA[<p>So had a fun business entity that needed a write only set of data that contained no duplicates (involved a security auditing trail if you care to know). That's when I realized we didn't have anything like this in the BCL. So here's a fun little ISet&lt;T&gt; implementation that proxies any existing set to provide those semantics.</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/10/39/83/29/WriteOnlySet{T}.cs" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-10-39-83-29/WriteOnlySet_7B00_T_7D00_.cs">WriteOnlySet{T}.cs</a></p>]]></content:encoded>
    </item>
    <item>
      <title>Yes you can execute PARAMETERIZED sprocs with EF Fluent API Code Only (and more!)</title>
      <link>https://docs.microsoft.com/archive/blogs/schlepticons/yes-you-can-execute-parameterized-sprocs-with-ef-fluent-api-code-only-and-more</link>
      <pubDate>Mon, 15 Oct 2012 18:00:00 GMT</pubDate>
      <dc:creator><![CDATA[Jimmy Zimms]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/schlepticons/2012/10/15/yes-you-can-execute-parameterized-sprocs-with-ef-fluent-api-code-only-and-more/</guid>
      <description><![CDATA[This is really an add-on to this post here.
Turns out most people were only thinking that you could...]]></description>
      <content:encoded><![CDATA[<p>This is really an add-on to this <a href="http://blogs.msdn.com/b/schlepticons/archive/2012/02/16/yes-you-can-execute-sprocs-with-ef-fluent-api-code-only.aspx">post here</a>.</p>
<p>Turns out most people were only thinking that you could execute SQL strings only as opposed to detailed parameterized SQL statements. That's not correct. It's all supported and approved.</p>
<p><strong>Scenario 1. Using Parameterized SQL statements</strong></p>
<p>So this covers selects as well as invoking sprocs. If you have named parameters in your sql string EF is more than happy to take them and set values based on some very simple rules.</p>
<ol>
<li>The SQL contains a parameter name value markup item (e.g. @ParamName) in the query iteself OR</li>
<li>The SQL contains a .Net format string replacement token (e.g. {0}</li>
</ol>
<div>So take the following sample SQL, "SELECT * FROM MyTable WHERE Column1 = @Value" then you would simple pass the query parameters via a DbParameter type (appropriate for your underlying DbProvider EF is using) to the Database.SqlQuery&lt;T&gt;(String sql, params Object[] parameters) &nbsp;overload. Simply add an API to your DbContext based type resembling this-&gt;</div>
<div>
<pre><span style="font-family: 'courier new', courier;">public IEnumerable&lt;T&gt; SqlQuery&lt;T&gt;(String sql, params SqlParameter[] parameters)</span><br /><span style="font-family: 'courier new', courier;">{</span><br /><span style="font-family: 'courier new', courier;"> return parameters == null </span><br /><span style="font-family: 'courier new', courier;"> ? this.Database.SqlQuery&lt;T&gt;(sql)</span><br /><span style="font-family: 'courier new', courier;"> : this.Database.SqlQuery&lt;T&gt;(sql, parameters.Cast&lt;Object&gt;().ToArray()); // Removed covarient by creating Object array</span><br /><span style="font-family: 'courier new', courier;">}</span></pre>
</div>
<div>Your code will now be passing parameterized sql just like the EF generated LINQ queries do. This method is&nbsp;preferred over string replacement (option 2) as it removes SQL injection attacks and speeds SQL queries as parameterized SQL tend to have execution plans cached by SQL Server.</div>
<div>
<p>To call the above method you should simply create the appropriate parameter type and supply it to EF.</p>
<p><span style="font-family: 'courier new', courier;">var sql = "SELECT * FROM MyTable WHERE Column1 = @Value";</span><br /><span style="font-family: 'courier new', courier;">var parameters = new [] { new SqlParameter("@Value", SqlDbType.Varchar, 50) { Value = 'My Value' };</span><br /><span style="font-family: 'courier new', courier;">var results = context.SqlQuery&lt;Foo&gt;(mySql, parameters);</span></p>
<p>and Boom goes the dynamite! :)</p>
<p><strong>Scenario 2. Changing Column Names from SPROCS</strong></p>
<p>A lot of people seem to be complaining that the SqlQuery&lt;T&gt; method performs simple convention based mapping on the result set meaning that the column value in Foo will be mapped to a property named Foo on type T as defined in your code. Somehow everyone throws up their hands and says they can't use EF. NO that is not the case but you just need to think about what you are doing a little more and pull out some simple TSQL skills. Using sp_executesql with parameterized input gives you all the safety and power of raw SQL text (actually for those of you that don't know, even SQL text is actually turned into calls to sp_executesql by the SQL query parser so you're always stuck with it regardless!). Add in a little dash of table variables and a final SQL statement and you are set. No rewriting your sprocs to match your model; no changing your model to match your sprocs.</p>
<p>In this example I am using a simple select statement but just imagine that this was the select inside of your sproc and change the @sql to be "execute mySproc @value" and you are fine...</p>
<pre><span style="font-family: 'courier new', courier;">--declare @sql nvarchar(max) = 'execute mySproc @value' EXAMPLE SPROC EXECUTE STATEMENT</span><br /><span style="font-family: 'courier new', courier;">declare @sql nvarchar(max) = 'select newid(),@SystemName'</span><br /><span style="font-family: 'courier new', courier;">declare @value nvarchar(max) = N'an input value;</span><br /><br /><span style="font-family: 'courier new', courier;">DECLARE @ParmDefinition nvarchar(500) = N'@value nvarchar(500)';</span><br /><br /><span style="font-family: 'courier new', courier;">-- define the table variable to hold the query output. In this case I could<br /></span><span style="font-family: 'courier new', courier;">-- have performed modification from the underlying sproc result set but I didn't</span></pre>
<pre><span style="font-family: 'courier new', courier;">declare @results TABLE(</span><br /><span style="font-family: 'courier new', courier;">SystemId uniqueidentifier,</span><br /><span style="font-family: 'courier new', courier;">SystemName nvarchar(500)</span><br /><span style="font-family: 'courier new', courier;">)</span><br /><br /><span style="font-family: 'courier new', courier;">-- Execute the sproc and fill the table variable with the output</span><br /><span style="font-family: 'courier new', courier;">INSERT @results</span><br /><span style="font-family: 'courier new', courier;">execute sp_executesql @sql,@ParmDefinition, @value=@value</span><br /><br /><span style="font-family: 'courier new', courier;">-- Return the values coerced to your desired shapre.</span><br /><span style="font-family: 'courier new', courier;">SELECT SystemID, SystemName [Name] FROM @results</span></pre>
<pre></pre>
<pre><span style="font-family: arial, helvetica, sans-serif;"><br /></span></pre>
<pre><span style="font-family: arial, helvetica, sans-serif;"><br /></span></pre>
<pre><span style="font-family: arial, helvetica, sans-serif;">HE SHOOTS HE SCORES!! So engoy your EFing journey out there<br /></span><span style="font-family: arial, helvetica, sans-serif;">folks and remember, think about your entire toolkit when solving problems!</span></pre>
</div>]]></content:encoded>
    </item>
    <item>
      <title>A Case for Test Driven Development</title>
      <link>https://docs.microsoft.com/archive/blogs/schlepticons/a-case-for-test-driven-development</link>
      <pubDate>Tue, 04 Sep 2012 11:43:00 GMT</pubDate>
      <dc:creator><![CDATA[Jimmy Zimms]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/schlepticons/2012/09/04/a-case-for-test-driven-development/</guid>
      <description><![CDATA[Powerpoint outline for a class I used to teach regarding the topic of TDD. Recently was asked by a...]]></description>
      <content:encoded><![CDATA[<p><span>Powerpoint outline for a class I used to teach regarding the topic of TDD. Recently was asked by a coworker to use this and decided to share with the world. Download as use as needed for your own training.</span></p>
<p><span>Written in 2005 and references Visual Studio 2003 but points still hold.</span></p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/10/34/62/83/tdd.zip" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-10-34-62-83/tdd.zip">tdd.zip</a></p>]]></content:encoded>
    </item>
    <item>
      <title>Simple WCF netMsmq Example (or Step 1 to stop reinventing yet another MSMQ clone)</title>
      <link>https://docs.microsoft.com/archive/blogs/schlepticons/simple-wcf-netmsmq-example-or-step-1-to-stop-reinventing-yet-another-msmq-clone</link>
      <pubDate>Tue, 29 May 2012 10:12:00 GMT</pubDate>
      <dc:creator><![CDATA[Jimmy Zimms]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/schlepticons/2012/05/29/simple-wcf-netmsmq-example-or-step-1-to-stop-reinventing-yet-another-msmq-clone/</guid>
      <description><![CDATA[It's pretty obvious that MSMQ is a bit of a dark art out there. People know it exists, everyone...]]></description>
      <content:encoded><![CDATA[<p>It's pretty obvious that MSMQ is a bit of a dark art out there. People know it exists, everyone knows someone somewhere that knows a guy who once did something with it, and like practitioners of esoteric arts, the technology is looked upon with distrust. In fact to the point that I see team after team recreate, usually poorly, the infrastructure for a true transactional&nbsp;queuing system, almost always defaulting to some SQL Server implementation.</p>
<p>Let's start with this simple axiom: &nbsp;DO NOT BUILD YOUR OWN QUEUING SYSTEM UNLESS THAT'S WHAT YOU ARE HIRED TO DO!</p>
<p>By the time you get it designed, debugged, and actually working you'll find that you've just spent your entire dev budget on something that probably doesn't even have half the real features of MSMQ (or any other vendor product). And I am not talking about little used fringe cases you don't need. I am talking about core infrastructure requirements for asynchronous communication infrastructure: Poison message&nbsp;detection, scale out,&nbsp;manageability&nbsp;tools, WMI and monitoring, QoS, and on and on...</p>
<p>Personally I think that the reason that people shy away from MSMQ is simple and clear: the documentation sucks, real working samples are rare, the features have quirks that are not always known, and lastly&nbsp;asynchronous&nbsp;message based solutions are hard. Well, I cannot fix the last item, hard is hard and unless you know how to design a Saga based solution you're going to have trouble. But the others are not hard, they just need explaining and simple working solutions to show how it works. With that being the case I am going to show the most basic of systems, a client and a server leveraging WCF with MSMQ. Attached to this blog is a very simple application that allows a client to fire and forget a WCF message and a service to activate and transactionally process it. I believe that this code and configuration should be clear enough (there's little to it!) and should "just work". Just make sure that you have MSMQ installed on your machine and run Visual Studio in elevated admin mode (so you can create MSMQ objects on your first run).</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/10/31/14/05/MSMQ%20Example.zip" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-10-31-14-05/MSMQ-Example.zip">MSMQ Example.zip</a></p>]]></content:encoded>
    </item>
    <item>
      <title>There's no kill switch on awesome...</title>
      <link>https://docs.microsoft.com/archive/blogs/schlepticons/theres-no-kill-switch-on-awesome</link>
      <pubDate>Tue, 10 Apr 2012 10:11:48 GMT</pubDate>
      <dc:creator><![CDATA[Jimmy Zimms]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/schlepticons/2012/04/10/theres-no-kill-switch-on-awesome/</guid>
      <description><![CDATA[Been meaning to post this for a long while now. Pretty much sums me up :)


thanks to Scott Adams...]]></description>
      <content:encoded><![CDATA[<p>Been meaning to post this for a long while now. Pretty much sums me up :)</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/27/72/2235.151221_strip.gif" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-27-72/2235.151221_5F00_strip.gif"><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/27/72/2235.151221_strip.gif" original-url="http://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-27-72/2235.151221_5F00_strip.gif" border="0" alt="" /></a></p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/27/72/2235.151221_strip.gif" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-27-72/2235.151221_5F00_strip.gif"></a></p>
<p>thanks to Scott Adams and his wonderful wit!</p>]]></content:encoded>
    </item>
    <item>
      <title>Powershell Automation and Remoting (a c# love story)</title>
      <link>https://docs.microsoft.com/archive/blogs/schlepticons/powershell-automation-and-remoting-a-c-love-story</link>
      <pubDate>Fri, 23 Mar 2012 09:17:00 GMT</pubDate>
      <dc:creator><![CDATA[Jimmy Zimms]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/schlepticons/2012/03/23/powershell-automation-and-remoting-a-c-love-story/</guid>
      <description><![CDATA[OK all of you readers out there should know and love Powershell by now and you are using it...]]></description>
      <content:encoded><![CDATA[<p>OK all of you readers out there should know and love Powershell by now and you are using it constantly, right? If not get on with it! One of the most exciting things that came out of version 2 was the ability to perform remoting. Basically the ability to connect a session to a remote system and have the commands executed there. Think about that for a minute...</p>
<p>Powershell:&nbsp;finally a&nbsp;real scripting environment (those of us refugees from the Unix Wars finally can feel whole again) in windows but one that works in real live .Net objects!<br />Remoting: like telnet without the craptastic-ness leveraging WS-Management as infrastructure marshaling commands / responses securely across the network.</p>
<p>This is pure bliss - almost as if I was back in BSD again!</p>
<p>Now there's plenty of stuff on the interwebs about how to work with remoting sessions interactively. There's also some information on how to work with powershell's automation API (read: your CLR code is running and hosting powershell directly in process). What it doesn't seem is that there's any examples of working with powershell automation with a remote session out there. Or if there are, they are not working in what I expect is the most common use case:<br /><strong>- Machines in Active Directory with Trust relationships leveraging Kerberos for the interactive user credential</strong></p>
<p>So to fix this I've gone and whipped up a small sample application that will show you the extra "bits" you need to use / configure to remote your automation applications. This post expects that you have a working understanding of powershell and the automation API (or at least can get the essence of it from the very understandable API).</p>
<p>As anyone performing automation knows your entry point to hosting powershell lays in the System.Management.Automation assembly (This is normally found in the %Program Files x86%\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0 directory). The first thing you need to do is to figure out what computer you are going to connect to and build the appropriate Uri for the WS-Management endpoint. There's scant information on this out there but basically the format is "&lt;Scheme&gt;://&lt;ServerName&gt;:&lt;Port&gt;/wsman". The two schemes that are supported are "http" and "https" and by default the management port will be 5985. <strong>editorial note: this port can be changed. Don't do it!</strong> So let's see some code:</p>
<p><span style="font-family: courier new,courier;">var remoteComputer = new Uri(String.Format("{0}://{1}:5985/wsman", Scheme, ComputerName));</span></p>
<p>From there you need to create one of the RunspaceConnectionInfo types. There are two that come out of the box, NewProcessConnectionInfo and WSManConnectionInfo. You'll want the latter (obviously). This type has a constructor with a plethora of overloads. For the curious you have options to supply the PSCredentials here as well which is great if you have the credentials for the user to connect as which great if they need to be different from the interactive user OR you need to perform a double hop and Kerberos Delegation is not enabled.</p>
<p><span style="font-family: courier new,courier;">var connection = new WSManConnectionInfo(remoteComputer);</span></p>
<p>From there you need to create a runspace (pretty much par course for powershell automation) and supply your connection object and then open the connection.</p>
<p><span style="font-family: courier new,courier;">var runspace = RunspaceFactory.CreateRunstpace(connection);</span><br /><span style="font-family: courier new,courier;">runspace.Open();</span></p>
<p>From there create your powershell instance and bind it to the runspace (note: this is an IDisposable class so wrap it in a using statement or other construct)</p>
<p><span style="font-family: courier new,courier;">var powershell = PowerShell.Create())</span><br /><span style="font-family: courier new,courier;">powershell.Runspace = runspace;</span></p>
<p>From there it's all the standard powershell automation calls you are already familiar with (e.g. AddCommand, AddScript, Add* methods). Here I'll just assume I have a command already built in a string variable and will invoke it, returning the output of the command (as a collection os PSObjects).</p>
<p><span style="font-family: courier new,courier;">powershell.AddScript(command);</span><br /><span style="font-family: courier new,courier;">var results = powershell.Invoke();</span><br /><span style="font-family: courier new,courier;">foreach (var output in results.Where(o =&gt; o != null)</span><br /><span style="font-family: courier new,courier;">{</span><br /><span style="font-family: courier new,courier;">&nbsp; Console.WriteLine(output.ToString());</span><br /><span style="font-family: courier new,courier;">}</span><br /><span style="font-family: courier new,courier;">//Assume we're done</span><br /><span style="font-family: courier new,courier;">powerrshell.Close();</span></p>
<p>And that's that! As you can see there's really no mystery in combining Powershell Automation and Remoting. It's very straight forward to use and accomplish; In all likelyhood you'll spend more time getting your Powershell statements correct than worrying about automation issues =D). Now do be aware that you need to enable powershell remoting and all the appropriate firewall/security settings on the target computer. All this can easily be handled by the WinRM service with a handy and easy configuration statement. Simply run (on an elevated command prompt if UAC enabled) "WinRM quickconfig" and it will prompt you to make the required configuration changes with the default values. It supports a quiet mode and the ability to supply all parameters via the command line which is important say for installers and whatnot.</p>
<p>I've put together the world's ugliest console application to show off listing files in a given path on a specified computer via Powershell Automation Remoting.</p>
<p>&nbsp;</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/10/28/69/40/PowerShell%20Remoting.zip" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-10-28-69-40/PowerShell-Remoting.zip">PowerShell Remoting.zip</a></p>]]></content:encoded>
    </item>
    <item>
      <title>Estimating the size a cube</title>
      <link>https://docs.microsoft.com/archive/blogs/schlepticons/estimating-the-size-a-cube</link>
      <pubDate>Wed, 14 Mar 2012 14:31:00 GMT</pubDate>
      <dc:creator><![CDATA[Jimmy Zimms]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/schlepticons/2012/03/14/estimating-the-size-a-cube/</guid>
      <description><![CDATA[where the set of D is dimensions and the set of M is measures. Let F(x) calculate the number of...]]></description>
      <content:encoded><![CDATA[<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/27/72/6136.Capture.PNG" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-27-72/6136.Capture.PNG"><img border="0" alt="" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/27/72/6136.Capture.PNG" original-url="http://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-27-72/6136.Capture.PNG" /></a></p>
<p>where the set of <em>D</em> is dimensions and the set of <em>M</em> is measures. Let <em>F(x)</em> calculate the number of members and <em>G(x)</em> the size of the member.</p>
<p>&nbsp;</p>
<p>Debate.</p>
<p>&nbsp;<strong>edit:</strong> I should have noted this is the approximation of the size. Depending on storage system there's obviously overhead to this.</p>]]></content:encoded>
    </item>
  </channel>
</rss>