mxride
mxride

Reputation: 11

Table - Count Column

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

Output of Powershell script

Upvotes: 0

Views: 1699

Answers (3)

veefu
veefu

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

mxride
mxride

Reputation: 11

Was able to figure it out.

$value = 0
$t = foreach($Sites in $test)
{
$value = $value + $Sites.TotalEndpoints
}
Write-Host $value

Upvotes: 0

Ansgar Wiechers
Ansgar Wiechers

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

Related Questions