![如何对 Powershell 命令的输出进行求和](https://linux22.com/image/778289/%E5%A6%82%E4%BD%95%E5%AF%B9%20Powershell%20%E5%91%BD%E4%BB%A4%E7%9A%84%E8%BE%93%E5%87%BA%E8%BF%9B%E8%A1%8C%E6%B1%82%E5%92%8C.png)
我写了一行代码来总结所有 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