Author Posts

December 10, 2014 at 7:15 am

Hi,

I have a little concern about Out-GridView. Let me explain:
if you go for

Get-Process|Out-GridView

, you can now filter on standards columns as we get them by the Default View (DotNetTypes.format.ps1xml). First question, why don't we see raw data instead? It seems that Out-GridView came after a Format-Table, which is impossible. Second, if you try to filter the CPU values, you're not filtering numeric values (as with PM for example) but strings.
I tried to select my data first:

Get-Process|Select-Object Name,@{l="CPU";e={[double]$_.cpu}}|Out-GridView

Same result, even if a Get-Member tells me my Select returns a numeric in CPU.
The only thing that works, was to use a Select -ExpandProperty, which is not what I'm trying to do...

Any idea?

December 10, 2014 at 7:23 am

Out-GridView doesn't call the formatting system, so Format-Table is never called. Out-GridView can't, in fact, accept the output of a Format- command. Try it – you'll get an error.

However, formatting isn't the only thing that can modify what an object looks like. A type extension can also do so, and that runs before anything else.

What it uses to handle sorting, I couldn't tell you. That's buried in the code, and I don't have access to that ;).

December 10, 2014 at 10:57 am

I agree with that, but maybe my english was not good enough to explain the problem (sorry I'm french).
I know the 3 rules of formatting (Default View, Default Property Set, Number of Properties), and the fact that among the Out- cmdlets, only Out-Gridview doesn't want any Format- before (I like the [i]format right[/i] rule). With my last command, I can see with Get-Member, that objects are returned, not format data, and this is exactly what I want to push in Out-GridView. CPU property is a numeric, and this is what I want to filter, not a string...
I'm pretty sure there's a reason bury in the code (as for the reason why get-service doesn't act the same with Computername as parameter or pipeline, but this is another topic), but is there any option to do this filtering in any way that I don't know?

December 11, 2014 at 2:38 am

You can do the filtering, but it's a bit more messy. Also, what you'll see is that the number is truncated to two decimal points unless you format it as required, then cast it.

  

  $processes = Get-Process
  $array = $null

  [array] $array += $processes.ForEach{

  #Specify precision here, otherwise it will default to 2 decimal places.
  $CPU = "{0:N5}" -f $psItem.CPU 
  
  
  $hash = @{
    name = $psitem.name
    CPU  = [double]$cpu
    }
  New-Object -TypeName psobject -Property $hash 
  }

  $array | Out-GridView

December 11, 2014 at 4:17 am

Thanks, I have tried a lot of things but forget to test with new object.

I rewrite it as :

Get-Process|Where {$_.cpu -ne $null}|ForEach {New-Object -TypeName psObject -Property @{name=$_.Name;cpu=[double]$_.cpu}}|Out-GridView

and it works fine.

Note that if I don't cast as double, I got the same output as with Get-Process. Even if I now know a workaround, I still think there's something wrong with this CPU property...