Powershell how to sum time
I am running the Get-MailboxImportRequestStatistics script to pull the TotalDuration for each imported PST. I try then to sum them using the -sum dimension object, but keep getting "measure: input object" 00: 03: 00.4321755 "is not numeric". I'm guessing I need to format the output?
$mbxreqs = Get-MailboxImportRequest -BatchName csr2 | where {$_.status -eq "Completed"}
$TimeCollection =@()
foreach ($mbx in $mbxreqs) {
$mbxstat = Get-MailboxImportRequestStatistics -Identity $mbx.RequestGuid | Select-Object @{L="MigTime";E={$_.OverallDuration}}
$result = New-Object psobject
$result | Add-Member -MemberType NoteProperty -Name "MigTime" -Value $mbxstat.migtime
$TimeCollection += $result
}
($TimeCollection | measure -property migtime -sum).sum
source to share
Short answer
($TimeCollection | select -exp migtime | measure -prop TotalMilliseconds -sum).sum;
More details
Minimum playback
This recreates your scenario:
$timespan1 = New-TimeSpan $(Get-Date) $(Get-Date -month 12 -day 31 -year 2017);
$result1 = New-Object psobject;
$result1 | Add-Member -MemberType NoteProperty -Name "MigTime" -Value $timespan1;
$timespan2 = New-TimeSpan $(Get-Date) $(Get-Date -month 12 -day 31 -year 2017);
$result2 = New-Object psobject;
$result2 | Add-Member -MemberType NoteProperty -Name "MigTime" -Value $timespan2;
$TimeCollection = @( $result1, $result2 );
($TimeCollection | measure -property migtime -sum).sum;
The same error occured:
measure: The input object "148.00: 00: 00.0010041" is not numeric.
Decision
Sum of duration in milliseconds:
$TimeCollection `
| Select-Object -ExpandProperty migtime `
| Measure-Object -Property TotalMilliseconds -sum;
Sum of duration in days:
$TimeCollection `
| Select-Object -ExpandProperty migtime `
| Measure-Object -Property TotalDays -sum;
On one line with aliases:
($TimeCollection | select -exp migtime | measure -prop TotalMilliseconds -sum).sum;
Explanation
The error occurred because an attempt was made to perform an arithmetic operation on an object that does not support arithmetic operations. The solution was to expand the object and perform an arithmetic operation on one of its numeric properties.
source to share