PowerCLI

 View Only
Expand all | Collapse all

Customize and format number output

  • 1.  Customize and format number output

    Posted Sep 30, 2011 08:53 PM

    How can  I can format the output to divide by a number?

    For instance, often time the space displays as bytes, I always have to manually to divide them by 1024^3 to make it look friendly.

    Example below...

    Get-Datastore | Get-View | Select -ExpandProperty Summary | Select Name, Capacity, FreeSpace, @{N="Provisioned"; E={ ($_.Capacity - $_.FreeSpace + $_.Uncommitted) }}



  • 2.  RE: Customize and format number output

    Posted Sep 30, 2011 09:15 PM

    Luckily PowerShell has some builtin constants that can help you.

    You can use 1GB instead of 1024^3, like this

    Get-Datastore | Get-View | 
    Select -ExpandProperty Summary | 
    Select Name, Capacity, FreeSpace, 
        @{N="Provisioned"; E={ ($_.Capacity - $_.FreeSpace + $_.Uncommitted)/1GB }}

    And to format the number you can use the Round method

    Like this

    Get-Datastore | Get-View | 
    Select -ExpandProperty Summary | 
    Select Name, Capacity, FreeSpace, 
        @{N="Provisioned"; E={[Math]::Round(($_.Capacity - $_.FreeSpace + $_.Uncommitted)/1GB,1) }}

    Or you can let the Format operator do the formatting, but remember that you will end up with a String, not a decimal number.

    Get-Datastore | Get-View | 
    Select -ExpandProperty Summary | 
    Select Name, Capacity, FreeSpace, 
        @{N="Provisioned"; E={"{0:f1}" -f (($_.Capacity - $_.FreeSpace + $_.Uncommitted)/1GB) }}


  • 3.  RE: Customize and format number output

    Posted Oct 02, 2011 01:16 AM

    Nice. But Capacity and FreeSpace still showing up the standard format, adding /1GB will only alter the entity..



  • 4.  RE: Customize and format number output

    Posted Oct 02, 2011 06:42 AM

    Apparently I didn't understand your question correctly.

    Could you give an example of what you actually want to see then ?



  • 5.  RE: Customize and format number output

    Posted Oct 02, 2011 06:29 PM

    Sorry I will be specific. I don't have access to production at the moment, but here is an exmaple what I remember ...

    Name               Capacity               FreeSpace     Provisioned
    ----                    --------                    ---------                    -----------
    vlun05     1827880859          736885071          178387048
    vlun01 781005859     246282578      714370728
    vlun06 781005859         214952469      66929187

    I used to copy them in to excel and divide them by 1024^3. You provided a solution and it formats the last column.

    Name               Capacity               FreeSpace     Provisioned
    ----                    --------                    ---------                    -----------
    vlun05     1827880859          736885071          1.78
    vlun01 781005859     246282578      714.37
    vlun06 781005859         214952469      669.29

    Anyway I can format all the column?

    Thanks!



  • 6.  RE: Customize and format number output
    Best Answer

    Posted Oct 02, 2011 08:47 PM

    Ok, I see.

    You can use the same technique for the other 2 properties. Something like this

    Get-Datastore | Get-View | 
    Select -ExpandProperty Summary | 
    Select Name,
        @{N="Capacity"; E={[Math]::Round($_.Capacity/1GB,1) }}, 
        @{N="FreeSpace"; E={[Math]::Round(($_.FreeSpace/1GB,1) }},
        @{N="Provisioned"; E={[Math]::Round(($_.Capacity - $_.FreeSpace + $_.Uncommitted)/1GB,1) }}

    Another option is to use the New-VIProperty cmdlet and define these properties in GB.

    See the Datastore section in my New-VIProperty module, where you find the New-VIProperty statements to provide the CapacityGB, FreeGB and ProvisionedGB properties.



  • 7.  RE: Customize and format number output

    Posted Oct 03, 2011 09:35 PM

    Thanks!! Without the New-VIProperty, the code is messy. I am going to look into the new cmdlets.

    Is there any book you can recommend for Powershell \ VMware PowerCLI?



  • 8.  RE: Customize and format number output

    Posted Oct 04, 2011 05:20 AM

    I list several resources in the My PS library post



  • 9.  RE: Customize and format number output

    Posted Oct 06, 2011 02:34 PM

    Thanks alot. That was helpful.



  • 10.  RE: Customize and format number output

    Posted Aug 13, 2012 04:45 PM

    I expanded the code as the following:

    @{N="Capacity"; E={[Math]::Round($_.Capacity/1GB,1) }},
    @{N="Provisioned"; E={[Math]::Round(($_.Capacity - $_.FreeSpace + $_.Uncommitted)/1GB,1) }},
    @{N="ProvFreeSpace"; E={[Math]::Round(($_.FreeSpace - $_.Uncommitted)/1GB,1) }},
    @{N="FreeSpace"; E={[Math]::Round($_.FreeSpace/1GB,1) }}

    but now it doesn't show the result in columns anymore, instead it is showing rows


    Name          : lun17
    Capacity      : 1999.8
    Provisioned   : 1731.8
    ProvFreeSpace : 268
    FreeSpace     : 1296.3

    Name          : lun07
    Capacity      : 1871.8
    Provisioned   : 1745.2
    ProvFreeSpace : 126.5
    FreeSpace     : 773.1

    Name          : lun04
    Capacity      : 1871.8
    Provisioned   : 1854.4
    ProvFreeSpace : 17.3
    FreeSpace     : 724

    Any idea how to make them in column again?

    Is there anyway I can filter the result - for instance, show the luns only if they have 500+?



  • 11.  RE: Customize and format number output

    Posted Aug 13, 2012 04:52 PM

    Hi there,

      try to put the |format-table -autosize at the end of what are you doing.

    I think that when using more and more stuff in select, it automatically goes to the format-list mode because it can't fit in the columns.

    You can force him to still show the table but : format-table

    Just remeber that when viewing its ok to use format-table, but if you would to do something more with that after the format-table in pipe, like export-csv you have to omming the format-table as this will modify objects as well and transform then to other type than in select.

    @{N="Capacity"; E={[Math]::Round($_.Capacity/1GB,1) }},

    @{N="Capacity"; E={ if ([Math]::Round($_.Capacity/1GB,1)) -ge 500 {[Math]::Round($_.Capacity/1GB,1)}  }   },

    if Capacity is greaterr or equal 500  put the capacity , if not "" will be put.

    Greg



  • 12.  RE: Customize and format number output

    Posted Aug 13, 2012 06:08 PM

    PowerShell tries to create the console output intelligently.

    If you have to many properties selected, the PS engine will automatically switch to the Format-List type of output.

    When you insist on Format-Table, the last column will be truncated.

    You can also wrap the table output over moe than 1 line by using Format-Tabel -AutoSize -Wrap. It's probably not ideal for long list, but hey, it's still a table.

    Have a look at Using Format Commands to Change Output View, it shows most of the available options.

    I prefer to do the filtering, with a Where-clause, before the Select, that avoids useless calculations.

    Somethin like this

    Get-Datastore | Get-View | 
    Select -ExpandProperty Summary | where {$_.Capacity -ge 500GB} | 
    Select Name,
        @{N="Capacity"; E={[Math]::Round($_.Capacity/1GB,1) }},     @{N="Provisioned"; E={[Math]::Round(($_.Capacity - $_.FreeSpace + $_.Uncommitted)/1GB,1)}},
        @{N="ProvFreeSpace"; E={[Math]::Round(($_.FreeSpace - $_.Uncommitted)/1GB,1) }},
        @{N="FreeSpace"; E={[Math]::Round(($_.FreeSpace/1GB,1)}}    


  • 13.  RE: Customize and format number output

    Posted Aug 15, 2012 09:10 PM

    Beautilful! I added another filter and the query takes about 7 seconds instead of the 1/2 second with "get-datastore *", Is there anyway I can optimize the query?

    Get-Datastore sg*| Get-View |where {$_.Name -match "lun|template"} | Select -ExpandProperty Summary | Select Name,
        @{N="Capacity"; E={[Math]::Round($_.Capacity/1GB,0) }},
        @{N="Provisioned"; E={[Math]::Round(($_.Capacity - $_.FreeSpace + $_.Uncommitted)/1GB,0) }},
    @{N="ProvFreeSpace"; E={[Math]::Round(($_.FreeSpace - $_.Uncommitted)/1GB,0) }},
    @{N="FreeSpace"; E={[Math]::Round($_.FreeSpace/1GB,0) }}| Sort-Object ProvFreeSpace -descending|
    format-table -AutoSize -Wrap

    I noticed if the disks type are mix of thick /thin, the calculations for the provisioned would be off.

    get-harddisk *template|select StorageFormat,persistence,disktype,CapacityKB|format-table

    StorageFormat                   Persistence                      DiskType                    CapacityKB
    -------------                   -----------                      --------                    ----------
             Thick                    Persistent                          Flat                      41943040
              Thin                    Persistent                          Flat                      20971520
              Thin                    Persistent                          Flat                      26214400
              Thin                    Persistent                          Flat                      52428800

    Name          Capacity Provisioned ProvFreeSpace FreeSpace
    ----          -------- ----------- ------------- ---------
    vtemplate      200         203            -3       140


  • 14.  RE: Customize and format number output

    Posted Aug 15, 2012 09:24 PM

    You can replace

    Get-Datastore sg*| Get-View |where {$_.Name -match "lun|template"}

    with

    Get-View -ViewType Datastore -Filter @{"Name"="^sg.*(lun)|(template)"}

    That should make the query a bit faster