CodeOwls Skunkworks at the AZ PoSh User Group

§ April 30, 2013 09:17 by beefarino |

white-compass-rose-hiTomorrow evening I’ll be giving an online presentation to the Arizona PowerShell User Group.  I’ve heard great things about this group and am really looking forward to the talk – if you want to join the fun check out the details here:

The topic is “Strange Things with PowerShell”.  Well, that’s the title – the topic is really a set of skunkwork projects I’ve been hammering out since the beginning of the year.  I’ll introduce them briefly in this post, but if you want to learn more you’ll have to tune in tomorrow evening or wait for the release.

The first project I’ll be covering is the Entity Shell (ES).  ES is a PowerShell module that “knows” all about Microsoft’s ORM named Entity Framework.  Basically, once you have an Entity Framework data context defined, you can use PowerShell to manage your entities without doing any extra work.  Here’s a working code snippet to whet your appetite:

# pull in the entity shell
import-module entityshell;

# pull in my entity data context
new-psdrive -name ent -root '' -psp entityprovider `
    -context [SuperAwesomeWebsite.Models.Context]

# create a set of 100 new user accounts for testing
0..99 | new-item -path ent:/users -username { "User$_" } -password { "Password$_" }

# commit the new entities to persistent storage

# server-side filter for users without a password
$u = dir ent:/users -filter "it.Password IS NULL" 

# generate a report of these users
$u | convertto-csv | out-file "badusers.csv"

# remove the offending users
$u | remove-item

# commit these removals to persistent storage

The other project I’ll be demoing is Polaris; this project came about as a way to see how simple I could make the process of extending Windows Explorer.  Shell namespace extensions are hard; Polaris makes it dirt simple, and all you need is a little PowerShell to turn Windows Explorer into a rich dashboard of the stuff you want to see.

More on these projects in the coming weeks; in the meantime, if you want to see the goods, you’ll have to tune in (

Proposed CodeStock Talks for 2013

§ April 23, 2013 12:33 by beefarino |

CodeStock 2013It’s that time of year again!  One of my favorite conferences has opened early registration, and once you register you get to vote on the sessions you want to see.  I’ve changed the theme of my submissions this year – instead of focusing on PowerShell stuff I’ve submitted several more general talks about developer and independent life.  I’m expounding on them here in case you need a little persuasion to send some votes my way.

An Honest Look at Being Independent

This talk is about the changes that comes with being independent of an employer.  The goal is give you a very clear picture of what changed for me – financially, psychologically, and legally.  Part of the talk will include a near-pornographic look at my cashflow and expenses – where the money comes from and where the money goes, and how this folds into the choices I make around my time and my family.

If you’re considering the move to independence, please vote for this session.

Programmer Body Issues

We don’t usually think of the programmer’s career ending because of injury, but it happens more often than you know.  The human body wasn’t designed to sit and type for long periods, and the effects of doing that can have career-ending implications.  This talk is about what your job is doing to your body, how to recognize the warning signs before stress turns into pain and then injury, and how to keep yourself healthy and happy and productive with very little effort.

This isn’t about diet & exercise, nor ergonomics.  It’s about undoing the damage you’ve already done.

Giving it Away: How Publishing OSS Helps the Independent Developer

It may not make sense at face-value, but spending your time on open-source projects can be a huge boon for your business.  This talk is about treating OSS as a business opportunity, and how I’ve managed these give-it-away efforts to generate more income than the “regular” consulting and software sales facets of my business.

Creating Mayhem!

This one is just for fun – Mayhem is an open-source project that allows you to “wire up” events to reactions.  Use your phone to turn on your lights, or your XBox controller to run a PowerPoint presentation.  I’ll show you how easy it is to get started with this framework, and how quickly you can fold in your own creations.

For our summer project this year, my oldest daughter and I will be creating devices for our dog’s kennel that will allow us to dispense treats, sense his movement and barking, etc.  Part of this talk will feature how we’re using Mayhem to read from and drive these arduino-based devices.

PowerShell Mini-game: Equality (Answer and Winner)

§ April 17, 2013 19:38 by beefarino |

Many thanks to everyone who contributed answers to this little puzzler!  I was actually a bit surprised at the volume - hopefully you found it fun and educational.

First, let’s get a definitive answer down for this one…


In PowerShell, when is the following statement true? Explain why.

     ( $a -eq $b ) -ne ( $b -eq $a )


At first glance this may not seem possible.  The trick is in understanding how PowerShell evaluates the equality operation when the types of $a and $b vary. 

There are two general cases to consider here: when $a is scalar, and when $a is an array.

Scalar Equality and Type Conversion

If you’re not familiar with the term scalar, I’m using it to mean a variable containing “a single value” as opposed to an enumerable collection type, such as an array or list.  Yeah, it’s from Perl, so what?  (Note that the string type, while technically an enumerable collection of characters, is considered a scalar value here.) 

Type the following in your PowerShell console (seriously (I SAID DO IT)):

$a = 0;
$b = '';
$a -eq $b

What do you get?  True!  Which makes sense, since 0 and an empty string both sort of mean “nothing” or “no result,” right?  Now type this:

$b –eq $a

Boom goes the dynamite: False.  So what’s happening here?  It may help to replace the variables with their actual values:

0 –eq '' # true case
'' -eq 0 # false case

When PowerShell evaluates this comparison, it begins by taking the left operand at face value.  Since the right operand is of a different type, PowerShell is forced to coerce its type to match the type of the left operand.  The reason for the false case becomes fairly obvious when you spell out the conversion PowerShell is doing on your behalf:

0 –eq [int]'' # true case
'' –eq [string]0 # false case

In the true case, the conversion of an empty string into an integer value of 0 makes sense, and the equality expression is true.  In the false case, the conversion of the number 0 into a string results in a string containing a single character ‘0’, which is different than the empty string and the expression is false.  You can verify these conversions in your console:

[int]''   # 0
[string]0 # 0

This hidden coercion can lead to some fairly interesting results, to put it mildly.  Try this one, it’ll blow your mind:

$a = $null;
$b = [string]$null;
$a -eq $b

Yep, that’s right: ($null) doesn’t equal ($null cast to a string) in PowerShell.  Have you ever passed $null to a .NET method that accepts a string?  No, you haven’t: you’ve passed an empty string without knowing it…

Arrays and –eq

Several puzzlers pointed out that –eq behaves differently when the left operand is an array; while not the answer I was looking for it’s just as correct as the previous one.

The easiest way to describe this is by example; try this in your PowerShell host of choice:

$a = 1,2,3,4,5;
$b = 3;
$a -eq $b
$b -eq $a

When the left operand is an array, –eq returns the members of the array that equal the right operand.  In this case, $a –eq $b returns the integer 3.  Reversing the order of operands naturally changes the expression, and since there is no way to convert an array of numbers to a single integer the comparison is false.

This is an awesome example of PowerShell’s penchant for lists and enumeration, and it applies to many of the binary operators it supports:

'aabb','bbcc','zzaa' -replace 'a','q'

The Takeaway

The lesson here is that while PowerShell isn’t a strongly-typed language like C#, it does have solid roots in the .NET type system and leverages it in many interesting and “helpful” ways.  If you’re not careful, you’ll get burned.


Ah, I almost forgot!

Choosing the winner on the merit of the answers turned out to be tough – everyone did a really great job.  Some explanations were better than others, but it was difficult to isolate one as the best.  Thankfully I have PowerShell's get-random to turn this into a “business decision.” 

And the author of the winning answer and receiver of a license for the immensely cool SeeShell software module published by the devastatingly handsome folks at Code Owls is….




Thanks for playing everyone - since this was so much fun let’s say we do it again soon.  For now though, focus on those upcoming Community Scripting Games!


PowerShell Mini-game: Equality

§ April 9, 2013 14:06 by beefarino |

In light of the upcoming community Scripting Games, I have a PowerShell brain teaser for you.  

Query: In PowerShell, when is the following statement true? Explain why.

     ( $a -eq $b ) -ne ( $b -eq $a )

Submit your answer as a comment, or post it on your own blog and provide a link in the comments of this post.  The best explanation (as chosen by me) will receive a complimentary single-user license for SeeShell (a $249 value).  You have until April 16, 2013 to submit your answer; the winner will be announced April 17, 2013.