Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell how to sum time

Tags:

powershell

I am running a Get-MailboxImportRequestStatistics script to pull the OverallDuration for each PST imported. I am trying to then sum them using the measure-object -sum but keep getting "measure : Input object "00:03:00.4321755" is not numeric." I assume I have 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
like image 857
Chabango Avatar asked Jun 10 '26 07:06

Chabango


1 Answers

Short Answer

($TimeCollection | select -exp migtime | measure -prop TotalMilliseconds -sum).sum;

Details

Minimal Reproduction

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 occurs:

measure : Input object "148.00:00:00.0010041" is not numeric.

Solution

Sum of duration in milliseconds:

$TimeCollection `
    | Select-Object -ExpandProperty migtime `
    | Measure-Object -Property TotalMilliseconds -sum;

Sum of durations in days:

$TimeCollection `
    | Select-Object -ExpandProperty migtime `
    | Measure-Object -Property TotalDays -sum;

In one line with aliases:

($TimeCollection | select -exp migtime | measure -prop TotalMilliseconds -sum).sum;

Explanation

The error occurred because of trying to run an arithmetic operation on an object that does not support arithmetic operations. The solution was to expand the object and run the arithmetic operation on one of its numeric properties.

like image 134
Shaun Luttin Avatar answered Jun 14 '26 06:06

Shaun Luttin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!