Creating a table

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of David DeHerrera David DeHerrera 10 months, 3 weeks ago.

  • Author
    Posts
  • #34138
    Profile photo of ertuu85
    ertuu85
    Participant

    Hello, I'm unsure how to do exactly what I'm thinking and I think the answer should be pretty plainly in front of me...

    
    $x = get-datastore | ?{$_.name -notlike "*local*"}
    Name                             FreeSpaceGB CapacityGB
    ----                                  -----------         ----------
    fake1                              172.830       1,023.750
    fake2                              226.777       1,023.750
    fake3                               95.774       1,023.750
    fake4                              152.063       1,023.750
    fake5                              196.396       1,023.750
    

    I want it to be listed like..

    Number      Name                             FreeSpaceGB CapacityGB
     -----            ----                                  -----------         ----------
       0              fake1                              172.830       1,023.750
       1              fake2                              226.777       1,023.750
       2              fake3                               95.774       1,023.750
       3              fake4                              152.063       1,023.750
       4              fake5                              196.396       1,023.750
    

    $x[0] would equal fake1

    What would be the easiest way to build this table?

  • #34140
    Profile photo of random commandline
    random commandline
    Participant
    get-datastore | ?{$_.name -notlike "*local*"} |
    select-object @{n='Number';exp={[regex]::Match($_.Name,'\d').value - 1}},
    FreeSpaceGB, CapacityGB
    

    Another solution is to increment (add 1 to number using ++ operator)

  • #34142
    Profile photo of ertuu85
    ertuu85
    Participant

    That produced some interesting results (took out the other columns)

    
     Number
     ------
         -1
         -1
         -1
         -1
         -1
         -1
         -1
         -1
         -1
         -1
         -1
         -1
         -1
         -1
         -1
         -1
         -1
         -1
         -1
         -1
         -1
          6
          6
    

    how would I go about doing an increment for the value of the first expression?

  • #34143
    Profile photo of ertuu85
    ertuu85
    Participant

    Got it doing

    get-datastore | ?{$_.name -notlike "*local*"} |
    select-object @{n='Number';exp={$global:counter; $global:counter++} },
    FreeSpaceGB, CapacityGB
    

    Thanks for your help!

  • #34144
    Profile photo of David DeHerrera
    David DeHerrera
    Participant

    This will be more than a one liner, but here you go.

    $DataStores = Get-Datastore | where {$_.Name -notlike "*local*"} | select @{N='Name'; E={$_.Name}}, @{N = 'FreeSpaceGB'; E={"{0:N0}" -f$_.FreeSpaceGB}}, @{N='CapacityGB'; E={"{0:N0}" -f $_.CapacityGB}}
    $Count = 0
    foreach($DStore in $DataStores)
    {
    
            $object = New-Object –TypeName PSObject
            $object | Add-Member –MemberType NoteProperty –Name "Number" –Value "$($Count)"
            $object | Add-Member –MemberType NoteProperty –Name "Name" –Value "$($DStore.Name)"
            $object | Add-Member –MemberType NoteProperty –Name "FreeSpaceGB" –Value "$($DStore.FreeSpaceGB)"
            $object | Add-Member –MemberType NoteProperty –Name "CapacityGB" –Value "$($DStore.CapacityGB)"
        $Count ++
        Write-Output $object
    }
    

You must be logged in to reply to this topic.