Import-Module ActiveDirectory
$ErrorActionPreference = "Continue"
$scriptThrottleLimit = 6
$scriptTimeoutLimit = 60
$enabledUsers = Get-ADUser -SearchBase $staffOU -Filter {Enabled -eq $true} -Properties SamAccountName, ipPhone Where-Object { $_.ipPhone.Length -eq 4 }
$enabledUsers | ForEach-Object -Parallel {
$error.Clear()
Get-ADPrincipalGroupMembership -Identity $_
} -ThrottleLimit $scriptThrottleLimit -TimeoutSeconds $scriptTimeoutLimit
normal parallel work with all objects
half of objects trowing error
InvalidOperation: The pipeline has been stopped.
> Get-Error
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鈥ion.PSTasks.PSTask:PSTask) [], PipelineStoppedException
FullyQualifiedErrorId : PSTaskException
if i set errorpreferedaction is stop, that i have module import error
Get-ADPrincipalGroupMembership:
Line |
4 | Get-ADPrincipalGroupMembership -Identity $_.SamAccountName
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| The 'Get-ADPrincipalGroupMembership' command was found in the module 'ActiveDirectory', but the module could not be loaded. For more information, run 'Import-Module ActiveDirectory'.
and here is get-error output
Type : System.Management.Automation.ActionPreferenceStopException
ErrorRecord :
Exception :
Type : System.Management.Automation.CommandNotFoundException
ErrorRecord :
Exception :
Type : System.Management.Automation.ParentContainsErrorRecordException
Message : The 'Get-ADPrincipalGroupMembership' command was found in the module 'ActiveDirectory', but the module could not be loaded. For more information, run 'Import-Module ActiveDirectory'.
HResult : -2146233087
TargetObject : Get-ADPrincipalGroupMembership
CategoryInfo : ObjectNotFound: (Get-ADPrincipalGroupMembership:String) [], ParentContainsErrorRecordException
FullyQualifiedErrorId : CouldNotAutoloadMatchingModule
InvocationInfo :
ScriptLineNumber : 4
OffsetInLine : 5
HistoryId : 1
Line : Get-ADPrincipalGroupMembership -Identity $_.SamAccountName
PositionMessage : At line:4 char:5
+ Get-ADPrincipalGroupMembership -Identity $_.SamAccountName
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
InvocationName : Get-ADPrincipalGroupMembership
CommandOrigin : Internal
ScriptStackTrace : at <ScriptBlock>, <No file>: line 4
CommandName : Get-ADPrincipalGroupMembership
TargetSite :
Name : TryModuleAutoDiscovery
DeclaringType : System.Management.Automation.CommandDiscovery, System.Management.Automation, Version=7.0.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
MemberType : Method
Module : System.Management.Automation.dll
StackTrace :
at System.Management.Automation.CommandDiscovery.TryModuleAutoDiscovery(String commandName, ExecutionContext context, String originalCommandName, CommandOrigin commandOrigin, SearchResolutionOptions searchResolutionOptions, CommandTypes commandTypes,
Exception& lastError)
at System.Management.Automation.CommandDiscovery.LookupCommandInfo(String commandName, CommandTypes commandTypes, SearchResolutionOptions searchResolutionOptions, CommandOrigin commandOrigin, ExecutionContext context)
at System.Management.Automation.CommandDiscovery.LookupCommandProcessor(String commandName, CommandOrigin commandOrigin, Nullable`1 useLocalScope)
at System.Management.Automation.ExecutionContext.CreateCommand(String command, Boolean dotSource)
at System.Management.Automation.PipelineOps.AddCommand(PipelineProcessor pipe, CommandParameterInternal[] commandElements, CommandBaseAst commandBaseAst, CommandRedirection[] redirections, ExecutionContext context)
at System.Management.Automation.PipelineOps.InvokePipeline(Object input, Boolean ignoreInput, CommandParameterInternal[][] pipeElements, CommandBaseAst[] pipeElementAsts, CommandRedirection[][] commandRedirections, FunctionContext funcContext)
at System.Management.Automation.Interpreter.ActionCallInstruction`6.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
Message : The 'Get-ADPrincipalGroupMembership' command was found in the module 'ActiveDirectory', but the module could not be loaded. For more information, run 'Import-Module ActiveDirectory'.
Data : System.Collections.ListDictionaryInternal
Source : System.Management.Automation
HResult : -2146233087
TargetObject : Get-ADPrincipalGroupMembership
CategoryInfo : ObjectNotFound: (Get-ADPrincipalGroupMembership:String) [], CommandNotFoundException
FullyQualifiedErrorId : CouldNotAutoloadMatchingModule
InvocationInfo :
ScriptLineNumber : 4
OffsetInLine : 5
HistoryId : 1
Line : Get-ADPrincipalGroupMembership -Identity $_.SamAccountName
PositionMessage : At line:4 char:5
+ Get-ADPrincipalGroupMembership -Identity $_.SamAccountName
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
InvocationName : Get-ADPrincipalGroupMembership
CommandOrigin : Internal
ScriptStackTrace : at <ScriptBlock>, <No file>: line 4
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](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
at System.Management.Automation.PowerShell.CoreInvoke[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
at System.Management.Automation.PowerShell.Invoke[T](IEnumerable input, PSInvocationSettings settings)
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 927
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 927
Message : The running command stopped because the preference variable "ErrorActionPreference" or common parameter is set to Stop: The 'Get-ADPrincipalGroupMembership' command was found in the module 'ActiveDirectory', but the module could not
be loaded. For more information, run 'Import-Module ActiveDirectory'.
Data : System.Collections.ListDictionaryInternal
Source : System.Management.Automation
HResult : -2146233087
Name Value
---- -----
PSVersion 7.0.3
PSEdition Core
GitCommitId 7.0.3
OS Microsoft Windows 10.0.14393
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0鈥
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
@PaulHigin Could you please look the issue?
Seeing a similar issue, any input would be much appreciated.
This is related to #12240
Currently, the session state of the script running foreach-object -parallel is not transferred to each runspace/thread started to handle parallel runs. Consequently the ActiveDirectory module for parallel run is loaded through command discovery. It is not clear what the actual problem is, but there are a number of ways a PowerShell module auto-load can fail.
I haven't yet started on issue #12240, but it should address this issue. The workaround is to manually load the ActiveDirectory module for each script run in parallel:
$enabledUsers = Get-ADUser -SearchBase $staffOU -Filter {Enabled -eq $true} -Properties SamAccountName, ipPhone Where-Object { $_.ipPhone.Length -eq 4 }
$enabledUsers | ForEach-Object -Parallel {
Import-Module ActiveDirectory
$ErrorActionPreference = "Continue"
$error.Clear()
Get-ADPrincipalGroupMembership -Identity $_
} -ThrottleLimit $scriptThrottleLimit -TimeoutSeconds $scriptTimeoutLimit
This issue has been marked as duplicate and has not had any activity for 1 day. It has been closed for housekeeping purposes.