3

I am trying a few things in Powershell and what I don't manage to achieve is the following (in Exchange):

Get-User | Get-MailboxStatistics

But in the output I would like some fields/outputs from the "Get-User" cmdlet and some fields/outputs from the "Get-MailboxStatistics" cmdlet.

If anyone has an answer, I have searched the web but with no success as I've had difficulties explaining it in a few words.

Thanks in advance for your help.

4 Answers 4

3

Start with the execution of one cmdlet, pipe the results to Foreach-Object and then save a reference to the current object ($user), now execute the second command and save it in a variable as well. Create new object with properties from both objects.

You also need to filter users that have mailboxes, use the RecipientTypeDetails parameter.

$users = Get-User -RecipientTypeDetails UserMailox 
$users | Foreach-Object{

    $user = $_
    $stats = Get-MailboxStatistics $user

    New-Object -TypeName PSObject -Property @{
        FirstName = $user.FirstName
        LastName = $user.LastName
        MailboxSize = $stats.TotalItemSize
        ItemCount =  $stats.ItemCount   
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

Hi Shay, I'm having the same issue whith your code: Pipeline not executed because a pipeline is already executing. Pipelines cannot be executed concurrently. + CategoryInfo : OperationStopped: (Microsoft.Power...tHelperRunspace:ExecutionCmdletHelperRunspace) [], PSInvalidOperationException + FullyQualifiedErrorId : RemotePipelineExecutionFailed There are some results but mixed with this reccuring error.
So your copde works if I do it in two times, first the "$users = Get-User -RecipientTypeDetails UserMailbox" and then " $users | For-each.......". I still wish there were a way to do this in one line.
Updated my answer, give it a try now.
@OlivierR Yes, that's a known issue and I wrote the code from memory.
Well, I am executing this remotely by .Net Framework code, and this does not seem to do the trick. However it does work in the shell, I'll just have to find the best way to retrieve those info remotely. Thank you for your input
|
2

I don't know if it is the best or optimal solution, but you certainly do it by saving actual user to variable in foreach:

$users = Get-User 
$users | % { $user = $_; Get-MailboxStatistics $_ | % 
    { 
        "User name:{0} - some mailbox statistics: {1}" -f $user.SomePropertyOfUser, $_.SomePropertyOfMailbox
    } 
}

The first step (saving users into separate variable) is required only when working with Exchange cmdlets - as mentioned here, you cannot nest Exchange cmdlets in foreach...

This error is caused when executing the Exchange cmdlets through PowerShell remoting, which do not support more than one pipeline running at the same time. You may see this error when you pipe the output from a cmdlet to foreach-object, which then runs another cmdlet within its scriptblock.

3 Comments

thanks for your astonishingly quick answer ! However the output is strange. I execute: Get-User | % { $user = $_; Get-MailboxStatistics $_ | % { $user.Name; $user.RecipientType; $_.displayname }} and the output is:
Pipeline not executed because a pipeline is already executing. Pipelines cannot be executed concurrently. + CategoryInfo : OperationStopped: (Microsoft.Power...tHelperRunspace:ExecutionCmdletHelperRunspace) [], PSInvalidOperationException + FullyQualifiedErrorId : RemotePipelineExecutionFailed EXCDEVMBX01 B0CA0B88-LGU000025 UserMailbox EXCDEVMBX01 B0CA0B88-LGU000025 EXCDEVMBX01 B0CA0B88-LGU000026 UserMailbox EXCDEVMBX01 B0CA0B88-LGU000026
Oh, I didn't expect that - I did not test my code... I will look at it
0
$users = Get-User  -RecipientTypeDetails UserMailbox
$users | Foreach-Object{ $user = $_; $stats = Get-MailboxStatistics $user.DistinguishedName; New-Object -TypeName PSObject -Property @{FirstName = $user.FirstName; LastName = $user.LastName;MailboxSize = $stats.TotalItemSize;ItemCount =  $stats.ItemCount  }}

I've had to add a specific field in input of Get-MailboxStatistics because remotely, I was having:

The following Error happen when opening the remote Runspace: System.Management.Automation.RemoteException: Cannot process argument transformation on parameter 'Identity'. Cannot convert the "gsx-ms.com/Users/userName1" value of type "Deserialized.Microsoft.Exchange.Data.Directory.Management.User" to type "Microsoft.Exchange.Configuration.Tasks.GeneralMailboxOrMailUserIdParameter".

Anyway, thank you both @Jumbo and @Shay-levy

Comments

0
Get-ADUser -identity ADACCOUNT | Select-object @{Name="Identity";Expression={$_.SamAccountName}} | Get-MailboxStatistics

For some reason the Identity parameter doesn't take pipelne input by value, only by property name. So in order to get it to work you can change the name of piped in data to match the parameter name of Identity. Then Get-MailboxStatistics finally knows how to treat the data your feeding it via the pipeline.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.