Wednesday, 17 June 2015

Powershell Error: Pipelines Cannot be Executed Concurrently

Problem:

Today I have received the Powershell error "Pipelines Cannot be Executed Concurrently" while running commands in Exchange Management Shell.
Pipeline not executed because a pipeline is already executing. Pipelines cannot be executed concurrently.
    + CategoryInfo          : OperationStopped: (Microsoft.Power...tHelperRunspace:ExecutionCmdletHelperRunspace) [],
   PSInvalidOperationException
    + FullyQualifiedErrorId : RemotePipelineExecutionFailed

Pipeline not executed because a pipeline is already executing. Pipelines cannot be executed concurrently.
    + CategoryInfo          : OperationStopped: (Microsoft.Power...tHelperRunspace:ExecutionCmdletHelperRunspace) [],
   PSInvalidOperationException
    + FullyQualifiedErrorId : RemotePipelineExecutionFailed
You can simply reproduce this error by running the following Exchange Powershell commands by two times.
Get-DistributionGroup | ForEach-Object {
$group = $_.Name
Get-DistributionGroupMember $_.Name | ForEach-Object {
      New-Object -TypeName PSObject -Property @{
       Group = $group
       Members = $_.Name
}}}

Fix/Solution: Pipelines Cannot be Executed Concurrently

In above Powershell, you can see that we are piping the output of Get-DistributionGroup to Foreach-Object and attempting to run the Get-DistributionGroupMember cmdlet.

The Get-DistributionGroupMember cmdlet is designed to accept pipeline input from Get-DistributionGroup, so there is no need to use Foreach-Object. You can just pipe Get-DistributionGroup to Get-DistributionGroupMember.
Get-DistributionGroup | Get-DistributionGroupMember
But the above command returns only members of all the Groups and doesn't return group name. Our need is to get both group names and members, so we supposed to use ForEach-Object. To fix this issue, we have to do it as two step process. First, we need to save the results of the Get-DistributionGroup cmdlet to a variable. Then we can pipe the variable to ForEach-Object.
$Groups = Get-DistributionGroup
$Groups | ForEach-Object {
$group = $_.Name
Get-DistributionGroupMember $_.Name | ForEach-Object {
      New-Object -TypeName PSObject -Property @{
       Group = $group
       Members = $_.Name
}}}

Advertisements
Advertisements

No comments:

Post a Comment