The pipeline has been stopped in parallel execution

Hello.

I have pipeline break when trying proceed hashtable keys to foreach-object in parallel. So it happens almost always, but some object gonna pass normally. I have no idea which object cause this problem, because it break before start execution. Keys dont have null object (i check it double)

code

$scriptThrottleLimit = 6
$scriptTimeoutLimit = 60
$groupsArrays = Get-ADGroup -SearchBase "OU=Groups,DC=domain,DC=local" -Filter {samaccountname -like "Department-*"} | Select-Object SamAccountName
$usersArray = @()

foreach ($group in $groupsArrays.samaccountname) {
    $usersArray += Get-ADGroupMember $group -Recursive | Select-Object samaccountname
}
foreach ($usr in $usersArray.SamAccountName){
    if (!($userGrpArray.ContainsKey($usr))) {
        $grp = Get-ADPrincipalGroupMembership -Identity $usr | Where-Object {$_.DistinguishedName -like 'CN=CID-*'} | Select-Object SamAccountName
        $userGrpArray.Add($usr,$grp)
    }
}
$userGrpArray.Keys | ForEach-Object -Parallel {
	    Write-Output $_
} -ThrottleLimit $scriptThrottleLimit -TimeoutSeconds $scriptTimeoutLimit

 

error

InvalidOperation: C:\Users\user\Desktop\activedirectory\Change-CIDUsers.ps1:75:1
Line |
  75 |  $userGrpArray.Keys | ForEach-Object -Parallel {
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | The pipeline has been stopped.

full error stack

Type        : System.Management.Automation.ActionPreferenceStopException
ErrorRecord : 
    Exception             : 
        Type        : System.Management.Automation.PipelineStoppedException
        ErrorRecord : 
            Exception             : 
                Type    : System.Management.Automation.ParentContainsErrorRecordException
                Message : The pipeline has been stopped.
                HResult : -2146233087
            CategoryInfo          : OperationStopped: (:) [], ParentContainsErrorRecordException
            FullyQualifiedErrorId : PipelineStopped
        TargetSite  : 
            Name          : Throw
            DeclaringType : System.Runtime.ExceptionServices.ExceptionDispatchInfo
            MemberType    : Method
            Module        : System.Private.CoreLib.dll
        StackTrace  : 
   at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(Object input)
   at System.Management.Automation.Runspaces.LocalPipeline.InvokeHelper()
   at System.Management.Automation.Runspaces.LocalPipeline.InvokeThreadProc()
        Message     : The pipeline has been stopped.
        Source      : System.Private.CoreLib
        HResult     : -2146233087
    TargetObject          : System.Management.Automation.PSTasks.PSTask
    CategoryInfo          : InvalidOperation: (System.Management.A…tion.PSTasks.PSTask:PSTask) [], PipelineStoppedException
    FullyQualifiedErrorId : PSTaskException
    InvocationInfo        : 
        ScriptLineNumber : 75
        OffsetInLine     : 1
        HistoryId        : -1
        ScriptName       : C:\Users\user\Desktop\activedirectory\Change-CIDUsers.ps1
        Line             : $userGrpArray.Keys | ForEach-Object -Parallel {

        PositionMessage  : At C:\Users\user\Desktop\activedirectory\Change-CIDUsers.ps1:75 char:1
                           + $userGrpArray.Keys | ForEach-Object -Parallel {
                           + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        PSScriptRoot     : C:\Users\user\Desktop\activedirectory\
        PSCommandPath    : C:\Users\user\Desktop\activedirectory\Change-CIDUsers.ps1
        CommandOrigin    : Internal
    ScriptStackTrace      : at <ScriptBlock>, C:\Users\user\Desktop\activedirectory\Change-CIDUsers.ps1: line 75
TargetSite  : 
    Name          : Invoke
    DeclaringType : System.Management.Automation.Runspaces.PipelineBase, System.Management.Automation, Version=7.0.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
    MemberType    : Method
    Module        : System.Management.Automation.dll
StackTrace  : 
   at System.Management.Automation.Runspaces.PipelineBase.Invoke(IEnumerable input)
   at System.Management.Automation.PowerShell.Worker.ConstructPipelineAndDoWork(Runspace rs, Boolean performSyncInvoke)
   at System.Management.Automation.PowerShell.Worker.CreateRunspaceIfNeededAndDoWork(Runspace rsToUse, Boolean isSync)
   at System.Management.Automation.PowerShell.CoreInvokeHelper[TInput,TOutput](PSDataCollection1 input, PSDataCollection1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.CoreInvoke[TInput,TOutput](PSDataCollection1 input, PSDataCollection1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.Invoke[T](IEnumerable input, PSInvocationSettings settings)
   at Microsoft.PowerShell.EditorServices.Services.PowerShellContextService.<>c__DisplayClass89_0`1.<ExecuteCommandAsync>b__0() in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShellContext\PowerShellContextService.cs:line 778
   at System.Threading.Tasks.Task`1.InnerInvoke()
   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location where exception was thrown ---
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.PowerShell.EditorServices.Services.PowerShellContextService.ExecuteCommandAsync[TResult](PSCommand psCommand, StringBuilder errorMessages, ExecutionOptions executionOptions) in
D:\a\1\s\src\PowerShellEditorServices\Services\PowerShellContext\PowerShellContextService.cs:line 778
   at Microsoft.PowerShell.EditorServices.Services.PowerShellContextService.ExecuteCommandAsync[TResult](PSCommand psCommand, StringBuilder errorMessages, ExecutionOptions executionOptions) in
D:\a\1\s\src\PowerShellEditorServices\Services\PowerShellContext\PowerShellContextService.cs:line 823
Message     : The running command stopped because the preference variable "ErrorActionPreference" or common parameter is set to Stop: The pipeline has been stopped.
Data        : System.Collections.ListDictionaryInternal
Source      : System.Management.Automation
HResult     : -2146233087

 

enviroment

PSVersion 7.0.3
PSEdition Core
GitCommitId 7.0.3
OS Microsoft Windows 10.0.14393

 

 

The error record contains: ‘The running command stopped because the preference variable “ErrorActionPreference” or common parameter is set to Stop’.

So there must be some problem in the data that is causing an exception. Try changing $ErrorActionPreference to ‘Continue’. You may get more information. At least you may see where the data is missing or broken.