Bug or ‘feature’? ;)

I’m in the middle of MS PowerShell training and today my colleague, Marcin, went into strange issue when working in WMI. Basically when he got two commands with select at the end of each he was getting only properties from first object collection. Second looked empty, or rather full of blanks. After we tried several things we discovered, that this is not WMI, nor second select that is causing this strange behaviour. Try yourself:

Get-Process | select Id            
Get-Service | select Name

Result? List of Ids and than many empty lines. This won’t happen if you try to launch each command separately. It won’t happen if you redirect first command to Out-Default (something I was sure was ALWAYS done automagically). It get’s even worse if you try to format output of second command, try:

Get-Process | select Id -First 2            
Get-Service | Format-List

You will get error similar to one you get when you e.g. try to sort after you format:

The object of type "Microsoft.PowerShell.Commands.Internal.Format.FormatStartData" is not valid or not in the correct sequence. 
This is likely caused by a user-specified "format-list" command which is conflicting with the default formatting.

I wonder if you walked into the same issue? Maybe it is already logged on connect? The only reasonable workaround I found so far is intentional redirection to out-default. What is most surprising to me is that I have never noticed that behaviour. And I select things quite often… Maybe that’s because I usually consume them and the real problem is selects left alone in the pipeline… 😉

PS: It was tested on W2K8R2 and Win7.

3 thoughts on “Bug or ‘feature’? ;)

  1. Seems to be a consequence of selecting single properties of alternating types. Possibly running off the screen because it’s not resetting the column width in between, and using two different justification methods?

    Try this, then switch the order of the outputs and see what happens:

    $a = “”|select @{name=”idi”;expression={1}}
    $b = “”|select @{name=”ids”;expression={“2”}}

  2. More testing. It seems to be some weirdness in how format-table works. Once it sets up a table, it keeps using it for all incoming objects as long as they remain of the same type. This produces one table:

    get-process l*;get-process w*

    not two separate tables. When you use select-object, the objects are all the same type (pscustomobject), but have different properties. If they don’t have any properties that match the table headings ft set up based on the first object, it becomes a null in the table.

    At least, that’s the way it appears to me.

  3. Thanks for testing it out. 🙂 I also think this is weird behavior of formating subsystem. Anyway – I guess we have to leave with this ‘feature’ and simply avoid mixing types in output of script/ function. Which is bad idea (and bad practice) anyway. 😉

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s