如何对 Powershell 命令的输出进行求和

如何对 Powershell 命令的输出进行求和

我写了一行代码来总结所有 Exchange 邮箱的空间。它就像

[PS] C:\Windows\system32>Get-Mailbox  | where {$_.RecipientType -eq "UserMailBox"} | Get-MailboxStatistics | measure-Obj
ect -Property TotalItemSize -sum
Measure-Object : Das Eingabeobjekt "42.32 MB (44,377,424 bytes)" ist nicht numerisch.
Bei Zeile:1 Zeichen:99
+ Get-Mailbox  | where {$_.RecipientType -eq "UserMailBox"} | Get-MailboxStatistics | measure-Object <<<<  -Property To
talItemSize -sum
    + CategoryInfo          : InvalidType: (42.32 MB (44,377,424 bytes):Unlimited`1) [Measure-Object], PSInvalidOperat
   ionException
    + FullyQualifiedErrorId : NonNumericInputObject,Microsoft.PowerShell.Commands.MeasureObjectCommand

正如所见,它不起作用,因为该对象的输出不是数值。那么,如何让 SUM 工作呢?

答案1

由于TotalItemSize是类型,Microsoft.Exchange.Data.ByteQuantifiedSize请尝试以下操作:

Get-Mailbox -RecipientTypeDetails UserMailbox | Get-MailboxStatistics | ForEach-Object {[Microsoft.Exchange.Data.ByteQuantifiedSize]::Parse($_.TotalItemSize)} | Measure-Object -Sum

相关内容