I've split this method up into posix and windows versions and heavily refactored both versions. With luck it'll be a bit less crazy to reason about what is happening.

In the long run I would really prefer to require callers to just provide a command and argument vector (especially since this code will only expand the first command, and if there's a shell pipeline it won't expand later commands) but I don't think we can get away with that for Facter 2.0. I would like to introduce a new execution method later on that requires an argument vector and explicitly deals with stdout/stderr which should make a lot of this more manageable.