powershell 未执行管道,因为管道已在执行

x33g5p2x  于 2023-05-29  发布在  Shell
关注(0)|答案(3)|浏览(250)

尝试运行脚本时,收到错误消息。
以下是我的脚本:

$saveto = "C:\scripts\Distribution Groups.txt"
filter get_member_recurse {
    if($_.RecipientType -eq "MailUniversalDistributionGroup") {
        Get-DistributionGroupMember -ResultSize "Unlimited" $_.Name | get_member_recurse
    } else {
    $output = $_.Name + " (" + $_.PrimarySMTPAddress + ")"
    Write-Output $output
    }
} 
$DistributionGroup = Get-DistributionGroup | Sort-Object Name | ForEach-Object {
    "`r`n$($_.DisplayName) ($($_.PrimarySMTPAddress))`r`n=============" | Add-Content $saveto
    $distout = Get-DistributionGroupMember -ResultSize "Unlimited" $_.Name | get_member_recurse
    Write-Output $distout | Sort-Object | Get-Unique  | Add-Content $saveto
}

错误信息:

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
 
Pipeline not executed because a pipeline is already executing. Pipelines cannot be executed concurrently.
    + CategoryInfo          : OperationStopped: (Microsoft.Power...tHelperRunspace:ExecutionCmdletHelperRunspace) [], PSInvalidOperationException
    + FullyQualifiedErrorId : RemotePipelineExecutionFailed
of1yzvn4

of1yzvn41#

最近遇到了这个错误,最终发现它来自FOREACH(或|%{})命令中的一个错误
最简单的解决方法是在对象进入foreach之前对其进行 Package
更改此:

one-command|two-command|%{stuff}

对此:

@(one-command|two-command)|%{stuff}

@()将它们收集到一个数组中

1hdlvixo

1hdlvixo2#

也许这就是你要找的?

$saveto = 'C:\temp\Distribution Groups.txt'
function get_member_recurse {
    Param([array]$Members)
    foreach ($Member in $Members) {
        if($Member.RecipientType -eq "MailUniversalDistributionGroup") {
           get_member_recurse (Get-DistributionGroupMember -ResultSize "Unlimited" $Member.Name)
        } else {
            [array]$output += $($Member.DisplayName + " (" + $Member.PrimarySMTPAddress + ")")
        }
    }
    return $output
}

$DistributionGroups = Get-DistributionGroup | Sort-Object Name
foreach ($dg in $DistributionGroups) {
    "`n$($dg.DisplayName) ($($dg.PrimarySMTPAddress))`n=============" | Out-File -FilePath $saveto -Append
    [array]$distout += get_member_recurse (Get-DistributionGroupMember -ResultSize "Unlimited" $dg.Name)
    $distout = $distout | Sort -Unique 
    $distout | Out-File -FilePath $saveto -Append
}
p5cysglq

p5cysglq3#

这里是同样的事情,但与进度条!!;-)

$saveto = 'C:\temp\Distribution Groups.txt'

function get_member_recurse {
    Param([array]$Members)
    foreach ($Member in $Members) {
        if($Member.RecipientType -eq "MailUniversalDistributionGroup") {
           get_member_recurse (Get-DistributionGroupMember -ResultSize "Unlimited" $Member.Name)
        } else {
            [array]$output += $($Member.DisplayName + " (" + $Member.PrimarySMTPAddress + ")")
        }
    }
    return $output
}

$DistributionGroups = Get-DistributionGroup | Sort-Object Name
foreach ($dg in $DistributionGroups) {
    write-Progress -Activity "Parsing Distribution Groups Recursively - $($DistributionGroups.IndexOf($dg)) of $($DistributionGroups.Count)" -status "$($dg.DisplayName) ($($dg.PrimarySMTPAddress))" -PercentComplete ($DistributionGroups.IndexOf($dg) / $DistributionGroups.count*100)
    "`n$($dg.DisplayName) ($($dg.PrimarySMTPAddress))`n=============" | Out-File -FilePath $saveto -Append
    [array]$distout += get_member_recurse (Get-DistributionGroupMember -ResultSize "Unlimited" $dg.Name)
    $distout = $distout | Sort -Unique 
    $distout | Out-File -FilePath $saveto -Append
}

相关问题