Powershell如何总结时间

时间:2017-08-04 17:19:44

标签: powershell

我正在运行Get-MailboxImportRequestStatistics脚本来为每个导入的PST提取OverallDuration。我试图使用measure-object -sum对它们求和,但继续得到" measure:输入对象" 00:03:00.4321755"不是数字。"我假设我必须格式化输出?

$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

1 个答案:

答案 0 :(得分:0)

简答

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

详细

最小复制

这会重新创建您的场景:

$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;

发生同样的错误:

  

measure:输入对象“148.00:00:00.0010041”不是数字。

<强>解决方案

持续时间总和(以毫秒为单位):

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

持续时间总和:

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

在一行中包含别名:

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

<强>解释

由于尝试对不支持算术运算的对象运行算术运算,因此发生错误。解决方案是扩展对象并对其中一个数字属性运行算术运算。