Powershell - Get-Mailbox + Get-MailboxStatistics

Hi experts,

I can't figure out where the mistake is in this cmdlet:

Get-mailbox | Where-Object {$_.RecipientTypeDetails -eq "SharedMailbox" } | ft DisplayName,@{e={ ((Get-MailboxStatistics $_.DisplayName).ItemCount)};Label="ItemCount"} -Autosize

I'm trying to pipline Get-MailboxStatistics thru get-mailbox cmdlets. If i use the above per user it works fine. But once i use it with filter or Where-Object i do not get results for ItemCount.

See output below:
[PS] C:\>Get-mailbox | Where-Object {$_.RecipientTypeDetails -eq "SharedMailbox" } | ft DisplayName,@{e={ ((Get-MailboxStatistics $_.DisplayName).ItemCount)};Label="ItemCount"} -Autosize

DisplayName                 ItemCount
-----------                 ---------
Mailbox1
Mailbox2
Mailbox3

Your advice much appreciated.
ExcelAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Simon Butler (Sembee)ConsultantCommented:
Use get-recipient at the start instead of get-mailbox

Simon.
0
Justin YeungSenior Systems EngineerCommented:
if you add -ShowError
 Get-mailbox | Where-Object {$_.RecipientTypeDetails -eq "SharedMailbox" } | ft DisplayName,@{e={ ((Get-MailboxStatistics $_.DisplayName).ItemCount)};Label="ItemCount"} -showerror

Open in new window

you will see the error message.
Failed to evaluate expression " ((Get-MailboxStatistics $_.DisplayName).ItemCount)".
    + CategoryInfo          : InvalidArgument: (Fimail_Admin:PSObject) [], CmdletInvocationException
    + FullyQualifiedErrorId : mshExpressionError


and it will work like below

Get-mailbox -Filter {RecipientTypeDetails -eq "SharedMailbox"} | Get-MailboxStatistics | select DisplayName,ItemCount

Open in new window

Note:
and I am not 100% sure why you have that error but which I believe when it passes the value via pipeline, it is already going to the next value and it is not fast enough to show up the info of getting get-mailboxstatistics. so the displayname returned and however the statistics isn't available at that moment yet. (that's why it goes blank)
0
ExcelAuthor Commented:
Simon, thanks for a suggestion. Still no output with Get-Recipient cmdlet.

[PS] C:\>Get-Recipient | Where-Object {$_.RecipientTypeDetails -eq "SharedMailbox" } | ft DisplayName,@{e={ ((Get-MailboxStatistics $_.DisplayName).ItemCount)};Label="ItemCount"}

DisplayName                                                 ItemCount
-----------                                                 ---------
Mailbox1
Mailbox2
Mailbox3
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

ExcelAuthor Commented:
Lustin, if i use -ShowError i don't seem to get any error displayed.

Re -Filter switch, I've tried that prior too. No luck but the result is a bit different. It returns ItemCount for the last line entry. See below.

[PS] C:\>Get-mailbox -filter {RecipientTypeDetails -eq "SharedMailbox"} | ft DisplayName,@{e={ ((Get-MailboxStatistics $_.DisplayName).ItemCount)};Label="ItemCount"}

DisplayName                                                 ItemCount
-----------                                                 ---------
Mailbox1
Mailbox2
Mailbox3                                                 25

Your suggested cmdlet with filter differs from what I'm working on. It won't return any results from Get-Mailbox parameters what are not part of Get-MailboxStatistics (eg ProhibitSendQuota or IssueWarningQuota)

Example:
it works if:
Get-mailbox -Filter {RecipientTypeDetails -eq "SharedMailbox"} | Get-MailboxStatistics | select DisplayName,ItemCount
                                       
it does not work if:
Get-mailbox -Filter {RecipientTypeDetails -eq "SharedMailbox"} | Get-MailboxStatistics | select DisplayName,ItemCount,IssueWarningQuota
0
Justin YeungSenior Systems EngineerCommented:
first this will not work since "IssueWarningQuota" is not a property of "Get-MailboxStatistic"
Get-mailbox -Filter {RecipientTypeDetails -eq "SharedMailbox"} | Get-MailboxStatistics | select DisplayName,ItemCount,IssueWarningQuota

Second
Get-mailbox -filter {RecipientTypeDetails -eq "SharedMailbox"} | ft DisplayName,@{e={ ((Get-MailboxStatistics $_.DisplayName).ItemCount)};Label="ItemCount"}

Filtering before pipeline is always faster since it doesn't process all the result before where-object
and as I mentioned before when it does the pipeline it might not be fast enough to do so.

however once you turns the search result to variable it will store in to your console memory
so then I will work as below

$Mailboxes = Get-mailbox -Filter {RecipientTypeDetails -eq "SharedMailbox"}
Foreach ($Mailbox in $Mailboxes)
{
Get-MailboxStatistics $Mailbox | select DisplayName,ItemCount,@{N="Quota";E={$Mailbox.IssueWarningQuota}}
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ExcelAuthor Commented:
Thanks Justin, it's exactly what i was looking for.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.