PSObject, and splatting / formatting Cim_LogicalDisk returns

Welcome Forums General PowerShell Q&A PSObject, and splatting / formatting Cim_LogicalDisk returns

This topic contains 3 replies, has 2 voices, and was last updated by

 
Participant
3 weeks ago.

  • Author
    Posts
  • #125034

    Participant
    Points: 42
    Rank: Member

    Hi, newbie to the forum.

    Given the classic Get-DiskInfo type of POSH query, how would I display the results in a table, if splatting properties to a PSObject?

    If I ship this across to a remote server:

    $disk = Get-CimInstance -Class CIM_LogicalDisk -CimSession $session | where drivetype -like 3
    The return is nominally:
    DeviceID DriveType VolumeName Size           FreeSpace 
    -------- --------- ---------- ----           -------- 
    C:       3         Drive C    498892533760   85959804928 
    E:       3         Drive E    24000140926976 14336094912512

    If I create a hash-table and a new PSObject

    $property = @{
    Name = $disk.name
    Label = $disk.volumeName
    Size = $disk.size
    Freespace = $disk.freespace
    }
    $object = New-Object PSObject -property $property
    The display is:
    PS C:\powershell> $object
    
    Label              Name      Size                           Freespace
    ----- -            ---       ----                           ---------
    {Drive C, Drive E} {C:, E:} {498892533760, 24000140926976} {385959804928, 14336094912512}

    1)  I'd like to get this to format as one might see in Format-Table,  that C and E are on separate lines.

    2) How would I format the Size and Freespace in GB (similarly to "{0:N2}"), where I get 2 decimal places?

    Many thanks,

    Seth

  • #125055

    Participant
    Points: 265
    Helping Hand
    Rank: Contributor

    The first part of your issue is that you have two objects, not one, but the way you're creating your final object means you're squashing them into a single object. This is what causes the lines to be merged; if you go about it differently, this won't happen.

    The second part is a trickier question. The easy answer is with a calculated property from Select-Object, which modifies the data to display what you want. I'll come back to the trickier answer in a moment.

    As you can see, you have a few options! But the core of the matter is that you have a collection of objects, so you either need to continue along the pipeline to enumerate them, or opt to enumerate them yourself with `foreach` or `ForEach-Object` when building your result objects. Multiple lines in a table = multiple output objects.

    The trickier answer I mentioned before is that you can keep your raw data in bytes if you wish and delegate the display to PS's formatting subsystem. Doing so is more complicated than I can really cover in a short forum post. Kevin Marquette covers it partially in one of his deep dive posts on PSCustomObject.

    • #125411

      Participant
      Points: 42
      Rank: Member

      Thank you for the ideas, Joel!  I'll give Kevin's presentation a good read.

      Generally, I have a Franken-script, hacked from Internet ideas, that uses Select-Object and the formatting math.  Personal challenge is to make it more elegant.

      Cheers,

      Seth

    • #125427

      Participant
      Points: 42
      Rank: Member

      Hi, Joel,

      I learned a lot from your code of building with a hash-table.  Many thanks for your explanation.  From your example, I can see the flow through the pipeline.

      What I didn't catch before was using "foreach-object".  I've not previously used this cmdlet, so I had to overcome a reading bias that it was the classic foreach.  Nope, two different things.

      https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/foreach-object?view=powershell-6

      I played with a few permutations of trying to get a pre-made PSObject fed into the script block using -ArgumentList, perhaps the way Invoke-Command can be fed a variable that exists locally

      -Scriptblock {param($some_param) Do-This -parameter $some_param} -ArgumentList $some_param

      Fun to try, just to see.  But, ForEach-Object doesn't seem work that way, learned that, moving on.

      Cheers,

      Seth

You must be logged in to reply to this topic.