A Dirt Simple PowerShell Hosting Example

§ October 15, 2014 13:40 by beefarino |

Earlier today I responded to a shout for help from twitter friend and fellow PowerShell hacker Tim Meers:

 

After sending Tim some code showing how to host simple PowerShell scripts in your application, I’ve received several requests from others for the same.  I figured it was worth a quick blog post.

The annotated code is below.  This host accepts an item path as input, and uses PowerShell to fetch and format the item as text.

using System;
using System.Linq;

// you'll need to add a reference to System.Management.Automation
using System.Management.Automation;
using System.Management.Automation.Runspaces;

namespace HostSample
{
    class Program
    {
        // to run this application, specify a path to an item
        //  e.g.:
        //      c:\
        //      env:/computername
        //      hkcu:/software
        static void Main(string[] args)
        {
            // create a RUNSPACE - this is used to maintain state between pipelines.
            //  e.g., variables, function definitions, etc
            using (var runspace = RunspaceFactory.CreateRunspace())
            {
                // open the runspace before you use it
                runspace.Open();

                // set the default runspace for the process;
                //  this is necessary for some features and cmdlets to work properly
                Runspace.DefaultRunspace = runspace;
                
                // create a POWERSHELL pipeline
                using (var powershell = PowerShell.Create())
                {
                    // assign the runspace to the pipeline
                    powershell.Runspace = runspace;

                    // build up the pipeline
                    powershell.AddCommand("get-item")
                        .AddParameter("path", args[0])
                        .AddCommand("format-table")
                        .AddCommand( "out-string");

                    // execute the pipeline
                    var results = powershell.Invoke();

                    // output the results
                    Console.WriteLine( results.FirstOrDefault() );
                }
            }
        }
    }
}

If you have any questions, please ask them in the comments.

Enjoy!



Rocket Science

§ October 14, 2014 15:01 by beefarino |

So, funny story in honor of Ada Lovelace day.

My first job was pretty amazing.  I worked with a bunch of really smart people doing really smart things.  Space things.  And optics things.  Now my role on this team was fairly meager – I basically helped the documentation team try and capture all of the smart things the other smarter people were doing – but it was still amazing to be around that stuff.  Space telescopes, satellites, cell phones, automotive components, this team helped design all sorts of useful and interesting crap.

Anyway, one woman on the engineering team – we shall call her Mary although that’s not her name – had a very sharp sense of humor and keen hearing, both of which come into play in this tale.  One evening we were out celebrating …. something – a release perhaps?  maybe a project wrap-up?  There was drinking and nogoodnicking.  One of my outside-of-work friends from my previous stint in grad school was there – let’s call him Joe.  This evening Joe happened to be hitting on Mary.  Hard.  I watched from my bar stool several meters away while he strutted about her like a peacock with a spare ego.  Mary was unimpressed and Joe eventually took the hint.

So Joe politely adjourns from Mary and joins me at the bar.  I asked him, “So, what do you think of Mary?  She’s kind of awesome, right?”

Joe sighed and said, “Yeah, she’s cute.  But she’s no rocket scientist.”

I started laughing.  That loud obnoxious laugh you may know.  Joe wanted to know why.  So I turned around and shouted, “Hey Mary, what is it you do for a living?”

She finished her beer, slammed the stein on the table, stood up to look Joe square in the eye and as she was leaving, she said:

I’m a fucking rocket scientist.

And the best part?  It was true.  100% honest.  She did rockets for a living.  A literal rocket scientist that sends things into space.  Ego Joe never stood a chance.

Re-Entry

So originally this post was a bit different.  It wasn’t a story, it was a rant.  I didn’t like the rant so I wanted to turn it into something positive. 

Here’s the thing – Ada Lovelace is important, and I’ve told my girls about her because I want them to know that women have had a profound impact on society.  Having that history is vital for them I think – I do believe the technical contributions from women are under-celebrated and often deliberately downplayed.  So I want them to know about Ada and what she contributed, and that we can literally thank her for my career and our family’s happiness.

But I know that won’t do anything real for them.  I mean, why should I expect stories of Ada to have a significant impact on my girls, when similar stories about C.S. Babbage had fairly milquetoast effects on me when I was their age.  Interesting, perhaps necessary, but it won’t light their fuse.

Which is why I spend so much time finding Marys.  Living people my girls can see doing real and amazing things, people they can talk to about wonders and interests.  Sure I want them to know that “once upon a time there was Ada,” but I think knowing that there is Mary right here, right now, doing really amazing stuff, is far more important to their mindset.

My biggest fear for my daughters is that they will come to believe that they can’t do something because of their gender or sex.  My biggest tool to counteract that fear, is to show them someone who can – someone like Mary.



Changes. I have them.

§ October 10, 2014 12:02 by beefarino |

As many of you know by now, I’ve accepted a position at Pluralsight as Curriculum Director. I can say honestly that I’ve never been more excited about taking a new position, and I’m really looking forward to being able to make a positive contribution at a fantastic organization. 

This represents yet another career shift for me.  Instead of slinging code day-to-day, I’ll be working to help others teach how to do so effectively.  As different as this will be, I feel like this position is the appropriate destination for my crooked and wandering career path.  My professional background didn’t start in technology; rather, it was cognitive psychology – specifically researching human perception and learning.  During that phase of my life, I came to love both learning and teaching. 

As I entered a technology career, I carried this with me; the opportunities to mentor for someone with no experience were limited of course.  It took years before I was comfortable sharing what I was doing with others, but I got there.  I started speaking at user groups, publishing open-source software, and eventually, I reached out to Pluralsight to see if they would be interested in having be as an author.

I didn’t end up producing a lot of content – all said and done, I will have four courses in the library.  And nothing terribly popular – but that’s not why I did it.  I missed teaching.  I wanted to get back into it somehow, the royalty model Pluralsight offers made that a reasonable avenue to follow, and frankly it was fun.  Thing is, as I worked through my courses, I noticed that I really enjoyed working with everyone on the Pluralsight side.  Every interaction with them was positive – even when they were turning down a course idea or pointing out my use of …. “sailor words.”  They seemed to appreciate the effort I put in too – eventually they folded me in as one of their peer reviewers, and for about two years I’ve been providing feedback on other courses as my schedule will allow.

Making the change from working for myself to working for someone else is something I honestly thought I would never choose to do.  I love the independent lifestyle, but there are some hard choices to make there.  Code Owls exists, in a nutshell, to allow me to manage my own time.  I want to be home for my kids to hear about their days, help with their schoolwork, and be their dad.  This new job doesn’t take me away from that – I still work primarily from home, my schedule is very flexible, and the company has a strong cultural focus on living a balanced life.  Moreover, growing a business by yourself is really hard.  I believe I could keep paying myself through Code Owls indefinitely, but I don’t think I could ever expect a raise unless I hired on, which is a hassle I don’t want and, frankly, can’t find the financial gain in doing so in today’s climate.  This shift represents a huge growth opportunity for me – one that would only be possible as part of an established organization.  That said I’m not closing Code Owls – I still plan to do things through this entity…

So what does this mean for the dozen or so open source projects I’ve pushed out there?  Honestly I think it means I will have more time and mental energy to devote to this area.  With clients consuming my tolerance for code, the production you see on my github page represents what is left over.  I fully expect to keep maintaining and growing StudioShell, and producing random PowerShell modules that only a few of you will actually use (wink-wink).  I also plan to keep speaking, after all, my love of teaching landed me this new amazing new gig, so why mess with that?

My official start date is November 17, 2014.  Until then, I’ll be putting bows on clients and projects in situ.

Oh, and before I forget – let me say THANK YOU to those of you who mentored me through this decision.  You know who you are.  And also a shout out to everyone for the well-wishes and kudos – the response to my initial announcement was overwhelming and I can’t respond to everyone individually.  Y’all have humbled me once again…



Two Notes to Self

§ October 6, 2014 10:29 by beefarino |

Dear Jim;

This letter is to inform you that my last day at Code Owls LLC will be Oct 31, 2014.  I have been offered a full-time opportunity to do amazing things with an amazing group of people, and I simply cannot turn away from this venture.  I will provide more details when I am able to do so, but if you appreciate how much I’ve enjoyed working with you, you will understand the potential of my new venture.

I know that self-improvement is something you value greatly.  In that vein, I would like to share a few thoughts with you about our time together. 

You are the best boss for which I have worked.  You are also the worst.  You have high expectations but balance them equally to my work and my life.  The amount of responsibility you place on my shoulders is immense – crippling at times, but the freedom you offer in exchange is worth it.  You are hyper-critical of my work, but at the same time you provide any resource necessary for me to succeed.  You expect me to work hard for my living, but don’t expect my work to be my life.

I’ve grown immensely these last five years with you.  Thank you for the opportunity to work together - I would welcome the chance to do so again.

Best regards,

Jim


Jim -

I’m sad at your decision, but I am definitely excited for you!  I know you did not come to this decision lightly – actually you tend to overthink these things if I’m being honest.  The value of your contributions to the company have been substantial, as evidenced by your being named “Employee of the Year” every year you’ve been with us!

I do put a lot of effort into becoming a better me, and I get the impression you do as well.  So let me share a few items that you might want to take with you as you leave our humble company…

You have certainly grown, but you’ll never be done learning.  That’s not an insult, it’s a compliment.  You don’t fear unknown things, and that’s how you succeed in this industry.  You’re not afraid to say “I don’t know” and that’s refreshing and necessary. 

You’ve learned what makes a “good decision.”  It’s not always what’s best for you, or for your employer; sometimes it’s the better of two bad choices.  Although I tend to question the processes behind your decisions, that isn’t an indication of mistrust.  It’s just how I do.

Also, you need to stop showing up to work in your robe.  It isn’t really a good idea, ever.  Seriously, stop doing it.  Put some pants on.

Despite the robe thing, I too would love to have you back with us at Code Owls LLC.  If you ever find yourself looking for another opportunity, please consider us.

Obliged,

Jim