Some small PowerShell bugs I’ve found lately

I love PowerShell. Everyone who knows me knows that. Recently, though, I seem to be running into more bugs. I’m not complaining, because PowerShell does tons of amazing things and the problems I’m encountering don’t have a huge impact. With that said, here they are.

Pathological properties in Out-GridView

PowerShell has always allowed us to use properties with names that aren’t kosher. For instance, we can create an object that has properties with spaces and symbols in the name like this:

$obj=[pscustomobject]@{'test property #1'='hello'}

This capability is essential, since we often find ourselves importing a CSV file that we don’t have any control over. (As an exercise, look at the expanded CSV output from schtasks.exe). To access those properties we can use quotes where most languages doesn’t like them.

$obj.'test property #1'

Or we can use variables (again, something most languages won’t let you do this easily):

$prop='test property #1'; $obj.$prop

A friend called me last week with an interesting issue which turned out to be related to this kind of behavior. He had a SQL query which renamed output columns in “pathological” ways. When he piped the output of the SQL to Out-GridView, the ugly columns showed up in the output, but the columns were empty.

Here’s a minimal case to reproduce the issue:

[pscustomobject]@{'test property.'='hello'} | out-gridview

The problem here is that the property name ends with a dot. Here’s a UserVoice entry that explains that Out-GridView doesn’t like property names that end in whitespace, either. I added a comment about dots for completeness’ sake.

Formatting remote Select-String output

Another minor issue I’ve run into is that deserialized select-string output doesn’t format nicely. The issue looks to be that the format.ps1xml for MatchInfo objects uses a custom ToString() method that doesn’t survive the serialization. What happens is that you just get blank lines instead of any helpful output. The objects are intact, though, all of the properties are there. So using the output is fine, just that the formatting is broken. Here’s a minimal example:

"hello`r`n"*6 | Out-File c:\temp\testFile.txt
write-host 'Local execution'
select-string -Path c:\temp\testfile.txt -Pattern hello -SimpleMatch  

write-host 'Remote execution'
invoke-command -ScriptBlock{ select-string -Path c:\temp\testfile.txt -Pattern hello -SimpleMatch} -ComputerName localhost   

I didn’t find anything close on UserVoice, so I posted a new entry.
Neither of these caused any real problem, but they were fun to dig into.

What bugs have you found lately? Have you reported them?


One Comment

  1. Pingback: Some small PowerShell bugs I've found lately - How to Code .NET

Leave a Reply

Your email address will not be published. Required fields are marked *