我有以下命令来计算一个属性的平均值
$Databases = Get-MailboxDatabase -Status
foreach($Database in $Databases) {
$DBSize = $Database.DatabaseSize
$MBCount = @(Get-MailboxStatistics -Database $Database.Name).Count
# This line is giving me trouble
$MBAvg = Get-MailboxStatistics -Database $Database.Name | %{$_.TotalItemSize.value.ToMb()} | Measure-Object -Average
New-Object PSObject -Property @{
Server = $Database.Server.Name
DatabaseName = $Database.Name
LastFullBackup = $Database.LastFullBackup
MailboxCount = $MBCount
"DatabaseSize (GB)" = $DBSize.ToGB()
"AverageMailboxSize (MB)" = $MBAvg.Average
"WhiteSpace (MB)" = $Database.AvailableNewMailboxSpace.ToMb()
Items = 0
LogicalSize = 0
}
}
我想让“测量对象”跟踪几个属性,总数和平均值。
$MeasureProps = "AssociatedItemCount", "DeletedItemCount", "ItemCount", "TotalDeletedItemSize", "TotalItemSize"
$Databases = Get-MailboxDatabase -Status
foreach($Database in $Databases) {
$DBSize = $Database.DatabaseSize
$MBCount = @(Get-MailboxStatistics -Database $Database.Name).Count
$MBStats = Get-MailboxStatistics -Database $Database.Name
#Expanded version of problem line above
foreach($mb in $MBStats)
{
foreach($prop in $MeasureProps)
{
#this is a random hack. I have almost no idea what I'm doing.
select $mb.$prop | Measure-Object -Property $prop -Sum -Average
}
}
New-Object PSObject -Property @{
Server = $Database.Server.Name
DatabaseName = $Database.Name
LastFullBackup = $Database.LastFullBackup
MailboxCount = $MBCount
"DatabaseSize (GB)" = $DBSize.ToGB()
"AverageMailboxSize (MB)" = $MBAvg.Average
"WhiteSpace (MB)" = $Database.AvailableNewMailboxSpace.ToMb()
Items = 0
LogicalSize = 0
}
}
我遇到的问题是如何编辑%{$_.TotalItemSize.value
以便我可以访问每个对象的总数。
答案 0 :(得分:2)
Measure-Object
适用于数值。问题是TotalItemSize和TotalDeletedItemSize值是字符串。要解决此限制,您可以使用Select-Object
cmdlet首先展开TotalItemSize和TotalDeletedItemSize的值,然后将结果传递给Measure-Object
:
$TotalItemSize = @{n='TotalItemSize';e={$_.TotalItemSize.Value.ToMB()}}
$TotalDeletedItemSize = @{n='TotalDeletedItemSize';e={$_.TotalDeletedItemSize.Value.ToMB()}}
Get-MailboxStatistics -Database 'Mailbox Database 0311695863' | `
Select-Object -Property AssociatedItemCount,DeletedItemCount,ItemCount,$TotalItemSize,$TotalDeletedItemSize | `
Measure-Object -Property AssociatedItemCount,DeletedItemCount,ItemCount,TotalItemSize,TotalDeletedItemSize -Sum -Average
答案 1 :(得分:1)
%
是ForEach-Object
的别名。如果我理解正确,您希望在Measure-Object
值集上使用TotalItemSize
。如果是,则应使用select
,这是Select-Object
的别名。这些方面的东西:
[...] | select -ExpandProperty TotalItemSize | Measure-Object [...]
当然,如果我没有正确理解你的问题,请纠正我;)
编辑:
我不知道一个干净/内置的方式来满足你的第二个问题的条件,我刚注意到(即传递多个属性)。我检查了ExpandProperty
并不是那么神奇,不允许你这样选择多个属性;)我建议稍微重写代码并通过属性进行枚举:
$MeasureProps = "AssociatedItemCount", [...], "TotalItemSize"
foreach($prop in $MeasureProps)
{
select $prop | Measure-Object -Property $prop -Sum -Average
}