Skip to content

Issue running Get-MgUser in v2.0.0-Preview5 - Collection was modified; enumeration operation may not execute. Possible PageSize issue in PS 5.1 #1916

Closed
@stukey

Description

@stukey

We have an existing PowerShell reporting script that we have recently migrated from the MSOL to Graph PowerShell SDK PS module. We are seeing the script regularly fail with a generic “An error occurred while sending the request” error. We see the same error for Get-MgUser and Get-MgDevice cmdlets. The cmdlets return results, but not all results that are available in the tenant. We are using the Microsoft Graph PowerShell SDK module version 2.0.0-preview5 (and also tested in preview8). We are authenticating using App Only authentication via custom AAD app Client Id and Client Secret (hence why we are using v2.00 rather than v1). We’re running under PowerShell 5.1. I believe the error could be related to PageSize since we have over 140,000 users and 200,000+ devices in our tenant. This seems similar to a previously reported issue: #1192, which was supposed to be fixed.

I've confirmed that setting the PageSize to 500 avoids the error in both the Get-MgUser and Get-MgDevice cmdlets.

Example code:

[array]$tmpUsers = Get-MgUser -All -Property City, Country, Department, OnPremisesProvisioningErrors, DisplayName, GivenName, OnPremisesImmutableId, OnPremisesLastSyncDateTime, Surname, LastPasswordChangeDateTime, AssignedLicenses, LicenseAssignmentStates, MobilePhone, Id, OfficeLocation, ProvisionedPlans, BusinessPhones, PostalCode, UsageLocation, DeletedDateTime, State, StreetAddress, SignInSessionsValidFromDateTime, JobTitle, UserPrincipalName, UserType, CreatedDateTime
 
Get-MgUser : An error occurred while sending the request.
 
At line:1 char:1
+ [array]$tmpUsers = Get-MgUser -All -Property City, Country, Departmen ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-MgUser_List], HttpRequestException
    + FullyQualifiedErrorId : Microsoft.Graph.PowerShell.Cmdlets.GetMgUser_List
 
$error | FL -Force
 
writeErrorStream      : True
Exception             : System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
                           at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
                           at System.Collections.Generic.Dictionary`2.Enumerator.MoveNext()
                           at System.Net.Http.Headers.HttpHeaders.<GetEnumerator>d__20.MoveNext()
                           at System.Net.Http.Headers.HttpHeaders.ToString()
                           at System.Net.Http.WinHttpHandler.AddRequestHeaders(SafeWinHttpHandle requestHandle, HttpRequestMessage requestMessage, CookieContainer cookies,
                        DecompressionMethods manuallyProcessedDecompressionMethods)
                           at System.Net.Http.WinHttpHandler.<StartRequestAsync>d__122.MoveNext()
                           --- End of inner exception stack trace ---
                           at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
                           at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
                           at Microsoft.Graph.RedirectHandler.<SendAsync>d__6.MoveNext()
                        --- End of stack trace from previous location where exception was thrown ---
                           at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
                           at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
                           at Microsoft.Graph.RetryHandler.<SendAsync>d__9.MoveNext()
                        --- End of stack trace from previous location where exception was thrown ---
                           at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
                           at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
                           at Microsoft.Graph.CompressionHandler.<SendAsync>d__2.MoveNext()
                        --- End of stack trace from previous location where exception was thrown ---
                           at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
                           at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
                           at Microsoft.Graph.AuthenticationHandler.<SendAsync>d__16.MoveNext()
                        --- End of stack trace from previous location where exception was thrown ---
                           at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
                           at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
                           at Microsoft.Graph.PowerShell.Users.<UsersUserListUser_Call>d__377.MoveNext()
                        --- End of stack trace from previous location where exception was thrown ---
                           at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
                           at Microsoft.Graph.PowerShell.Users.<UsersUserListUser_Call>d__377.MoveNext()
                        --- End of stack trace from previous location where exception was thrown ---
                           at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
                           at Microsoft.Graph.PowerShell.Cmdlets.GetMgUser_List.<on2Xx>d__86.MoveNext()
                        --- End of stack trace from previous location where exception was thrown ---
                           at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
                           at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
                           at Microsoft.Graph.PowerShell.Users.<UsersUserListUser_Call>d__377.MoveNext()
                        --- End of stack trace from previous location where exception was thrown ---
                           at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
                           at Microsoft.Graph.PowerShell.Users.<UsersUserListUser_Call>d__377.MoveNext()
                        --- End of stack trace from previous location where exception was thrown ---
                           at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
                           at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
                           at Microsoft.Graph.PowerShell.Users.<UsersUserListUser>d__375.MoveNext()
                        --- End of stack trace from previous location where exception was thrown ---
                           at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
                           at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
                           at Microsoft.Graph.PowerShell.Cmdlets.GetMgUser_List.<ProcessRecordAsync>d__84.MoveNext()
TargetObject          :
CategoryInfo          : NotSpecified: (:) [Get-MgUser_List], HttpRequestException
FullyQualifiedErrorId : Microsoft.Graph.PowerShell.Cmdlets.GetMgUser_List
ErrorDetails          :
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : at Get-MgUser<Process>, C:\Program Files\WindowsPowerShell\Modules\Microsoft.Graph.Users\2.0.0\exports\ProxyCmdletDefinitions.ps1: line 5854
                        at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo : {0, 1}
PSMessageDetails      :

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions