Reputation: 11
I have a PowerShell script that coverts JSON from an API and creates a table output. How would I TOTAL (add or count) the column 'TotalEndpoints' and display the total number of Enpoints?
Here is the creation of the table
(Invoke-RestMethod @Params).Sites | Format-Table SiteName,SiteId,TotalEndpoints
Upvotes: 0
Views: 1699
Reputation: 2890
Your loop will get the job done. You can also try Measure-Object
if you want to avoid duplicating for-loops for common calculations.
Try something like this:
$siteData = (Invoke-RestMethod @Params).Sites
$siteData | Format-Table SiteName,SiteId,TotalEndpoints
$GrandTotalEndpoints = ($siteData | Measure-Object -sum -property TotalEndpoints).Sum
Write-Host $GrandTotalEndpoints
Upvotes: 0
Reputation: 11
Was able to figure it out.
$value = 0
$t = foreach($Sites in $test)
{
$value = $value + $Sites.TotalEndpoints
}
Write-Host $value
Upvotes: 0
Reputation: 200193
Use Select-Object
for manipulating objects. Use Format-Table
only as the last step for the actual output.
$sites = (Invoke-RestMethod @Params).Sites |
Select-Object SiteName, SiteId, TotalEndpoints
$total = @($sites | Select-Object -Expand TotalEndpoints).Count
$sites | Format-Table
"Total: $total"
Upvotes: 2