Forum Discussion
bharatTechie
Jun 08, 2022Copper Contributor
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)}}
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
Sort By
- LainRobertsonSilver Contributor
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
- bharatTechieCopper 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.
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