function to return format as table

This topic contains 10 replies, has 4 voices, and was last updated by  Anthony Stringer 1 year, 3 months ago.

  • Author
    Posts
  • #38140

    selko
    Participant

    Hi,
    i have a small function

    function Get-FreeDiskSpace
    {
    $Items = Get-CimInstance -ClassName Win32_LogicalDisk

    $RetVal = foreach ($Item in $Items)
    {
    [pscustomobject]@{
    DeviceID = $Item.DeviceID
    DriveType = $Item.DriveType
    VolumeName = $Item.VolumeName
    ProviderName = $Item.ProviderName
    Size = "{0,7:#.00} GB" -f ($Item.Size/1GB)
    FreeSpace = "{0,7:#.00} GB" -f ($Item.FreeSpace/1GB)
    PercentFree = @(if ($Item.Size -gt 0) { "$([Math]::round((($Item.FreeSpace/$Item.Size) * 100))) %" } else { "0 %" })[0]
    SystemName = $Item.SystemName
    }
    }

    $RetVal
    }

    which returns values like this:
    DeviceID : C:
    DriveType : 3
    VolumeName : Windows
    ProviderName :
    Size : 118,63 GB
    FreeSpace : 44,54 GB
    PercentFree : 38 %
    SystemName : mimi

    DeviceID : D:
    DriveType : 3
    VolumeName : Volume
    ProviderName :
    Size : 1863,01 GB
    FreeSpace : 1344,11 GB
    PercentFree : 72 %
    SystemName : mimi

    I want the function to return values like Format-Table. But i still want to have access to the objects in the pipeline.

    What am i missing here ? 🙂

    Thanks

  • #38145

    Richard Diphoorn
    Participant

    I modified your code, and took out the variables. Try this:

    function Get-FreeDiskSpace
    {
      foreach ($Item in Get-CimInstance -ClassName Win32_LogicalDisk)
      {
        [pscustomobject]@{
          DeviceID     = $Item.DeviceID
          DriveType    = $Item.DriveType
          VolumeName   = $Item.VolumeName
          ProviderName = $Item.ProviderName
          Size         = '{0,7:#.00} GB' -f ($Item.Size/1GB)
          FreeSpace    = '{0,7:#.00} GB' -f ($Item.FreeSpace/1GB)
          PercentFree  = @(if ($Item.Size -gt 0) 
            {
              "$([Math]::round((($Item.FreeSpace/$Item.Size) * 100))) %" 
            }
            else 
            {
              '0 %' 
            }
          )[0]
          SystemName   = $Item.SystemName
        }
      }
    }
    

    Works perfectly when doing something like this then:

    Get-FreeDiskSpace | Format-Table
    
  • #38151

    Liam Kemp
    Participant

    Hi Selko,
    I got bogged down trying to do exactly the same thing not so long ago, until i got some great advice here. Don't forget the primary purpose of a function – To collect and process the information. The formatting comes later. Specifically, when the function is run. So don't worry about how the function is actually outputting the data, because this can be changed when the function is run by piping it to format-table.

  • #38152

    selko
    Participant

    Hi,

    @Richard: Thanks but my original code works the same like your code.
    @Liam: I understand that formatting should happen outside the function but i would like to change the default formatting (to Format-Table) like Get-Process it does.

    Maybe there is someone which has solved this...

    Thanks

  • #38153

    Richard Diphoorn
    Participant

    Selko, your question was not clear so I had to guess. My code is simpler and conforms to some community best practices. Now that i understand your question, look into this. It will answer what you need. https://technet.microsoft.com/en-us/library/hh847831.aspx

  • #38154

    selko
    Participant

    Hi Richard,

    thanks for the link. I hoped that it is possible somehow without creating extra files.

    Thanks

  • #38155

    Richard Diphoorn
    Participant

    As far as I know you have to use custom formatting files.

  • #38156

    Anthony Stringer
    Participant

    you would either have to do this outside the function like this

    # save to variable
    $freediskspace = Get-FreeDiskSpace
    
    # format for viewing in console
    $freediskspace | ft -a
    
    # do something else with it
    $freediskspace | epcsv c:\temp\freediskspace.csv -not
    

    or use an external file. it is not as difficult as it at first seems.

  • #38157

    selko
    Participant
  • #38158

    Richard Diphoorn
    Participant

    Ah great! Looks promising. Keep us updated!

  • #38160

    Anthony Stringer
    Participant

    Awesome! thanks for sharing – I had no idea

You must be logged in to reply to this topic.