문제

I have the sourcedata similar to the following denormalized table

DisplayName                  LogRecordCount LogRecordBytes
-----------                  -------------- --------------
Mailbox - Low,                    459          43756
Mailbox - Low,                    1628         185542
Mailbox - Low,                    2575         264474
Mailbox - Low,                     522          48813
Mailbox - Low,                     410         138212
Mailbox - Low,                    1057         200043
Mailbox - Freed                  3866        2170719
Mailbox - Freed                   606         370304
Mailbox - Freed                  4137        1939924
Mailbox - Freed                 3655        1654650

My goal is to write a summary similar to the following

DisplayName                  LogRecordCount  
-----------                  -------------- 
Mailbox - Low,                     6651            
Mailbox - Freed                   12264     

The following code is very close to getting what I need, but the "DisplayName" isn't appearing. The value of DisplayName is buried in the result of the Group-Object command.

$stats | ? {$_.DigestCategory -eq 'LogBytes'} | group MailboxGuid | %{
    New-Object psobject -Property @{
        MailboxGuid = $_.Name
        LogRecordBytes = ($_.Group | Measure-Object LogRecordBytes -Sum).Sum
        DisplayName   = $_.Group.DisplayName
    }
}| sort-object LogRecordBytes |  ft -a DisplayName, MailboxGuid, LogRecordBytes

Question

What is the proper syntax for DisplayName = $_.Group.DisplayName to display the display name in the results?

도움이 되었습니까?

해결책

Swap out your DisplayName = .. line with the following:

DisplayName = $_.Group | Select-Object -ExpandProperty DisplayName -Unique

If for some magical reason there are different DisplayName values in the group, you will get an array like {value1, value2}, but normally it will return a single name only :)

다른 팁

Graimer's answer is perfectly fine, but you could also just pluck the DisplayName from the first element of the group, assuming all group elements have the same value for DisplayName

DisplayName = $_.Group[0].DisplayName

Just wanted to let you know of another alternative.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top