Forum Discussion

bharatTechie's avatar
bharatTechie
Copper Contributor
Jun 08, 2022
Solved

Format-Table, only show conditional data or Sort-Object in powershell

Hi All,

 

I am trying to format data using format-table but I also want to display the results only if PercentageUsed is more than 50%. Any Ideas?

 

Get-AzVmUsage -Location australiaeast | Format-Table @{label="Name";expression={$_.name.LocalizedValue}}, ResourceType, CurrentValue, Limit, @{label="PercentageUsed";expression={[math]::Round(($_.CurrentValue/$_.Limit)*100)}}

 

 

 

  • bharatTechie 

     

    Get-AzVmUsage -Location australiaeast |
        Where-Object { $_.Limit -and $_.Limit -gt 0 -and $_.CurrentValue / $_.Limit -ge 0.5 } |
            ForEach-Object {
               [PSCustomObject] @{
                   Name = $_.name.LocalizedValue;
                   ResourceType = $_.ResourceType;
                   CurrentValue = $_.CurrentValue;
                   Limit = $_.Limit;
                   PercentageUsed = [math]::Round($_.CurrentValue * 100 / $_.Limit);
               }
            } | Format-Table -AutoSize

    Cheers,

    Lain

2 Replies

  • LainRobertson's avatar
    LainRobertson
    Silver Contributor

    bharatTechie 

     

    Get-AzVmUsage -Location australiaeast |
        Where-Object { $_.Limit -and $_.Limit -gt 0 -and $_.CurrentValue / $_.Limit -ge 0.5 } |
            ForEach-Object {
               [PSCustomObject] @{
                   Name = $_.name.LocalizedValue;
                   ResourceType = $_.ResourceType;
                   CurrentValue = $_.CurrentValue;
                   Limit = $_.Limit;
                   PercentageUsed = [math]::Round($_.CurrentValue * 100 / $_.Limit);
               }
            } | Format-Table -AutoSize

    Cheers,

    Lain

    • bharatTechie's avatar
      bharatTechie
      Copper Contributor

      LainRobertson Thank you so much. This is what I was looking for and ft was giving me a headache. Looks like this is the way to do it. :smile:

      Now I will sort the percentageused and show the highest used at the top and it will look awesome. 🙂

      Get-AzVmUsage -Location australiaeast |
      >>     Where-Object { $_.Limit -and $_.Limit -gt 0 -and $_.CurrentValue / $_.Limit -ge 0.2 } | 
      >>         ForEach-Object {
      >>            [PSCustomObject] @{
      >>                Name = $_.name.LocalizedValue;
      >>                ResourceType = $_.ResourceType;
      >>                CurrentValue = $_.CurrentValue;
      >>                Limit = $_.Limit;
      >>                PercentageUsed = [math]::Round($_.CurrentValue * 100 / $_.Limit);
      >>            }
      >>         } | Format-Table -AutoSize
      
      Name                       ResourceType CurrentValue Limit PercentageUsed
      ----                       ------------ ------------ ----- --------------
      Total Regional vCPUs                              62   250             25
      Standard Dv3 Family vCPUs                         16    30             53
      Standard BS Family vCPUs                          30   120             25
      Standard DSv4 Family vCPUs                         8    30             27

       

Resources