gpt4 book ai didi

Powershell如何计算时间总和

转载 作者:行者123 更新时间:2023-12-02 14:46:57 25 4
gpt4 key购买 nike

我正在运行 Get-MailboxImportRequestStatistics 脚本来提取每个导入的 PST 的整体持续时间。然后我尝试使用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

最佳答案

简短回答

($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 : Input object "148.00:00:00.0010041" is not numeric.

解决方案

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

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

说明

由于尝试在不支持算术运算的对象上运行算术运算而发生错误。解决方案是扩展该对象并对其数字属性之一运行算术运算。

关于Powershell如何计算时间总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45512352/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com