Calling the Get-MgUser and other Microsoft.Graph functions often cause my scripts to fail with this error:
format-default: There is no Runspace available to run scripts in this thread. You can provide one in the DefaultRunspace property of the System.Management.Automation.Runspaces.Runspace type. The script block you attempted to invoke was:
…
The problem is more reproducible on virtual machines with 2 virtual CPUs. It seems this occurs when the Microsoft.Graph objects are emitted to the pipeline.
Expected behavior
Emitting Microsoft.Graph object to the pipeline (or screen) should not terminate PowerShell scripts.
How to reproduce
Deploy a Hyper-V VM running Windows 11 with 2 vCPU and 4 GB RAM
7. Use Connect-MGGraph to connect.
8. Execute the script. For example:
`.\Test-format-default.ps1 -User "John Doe" -Provider Graph`
On a 2 vCPU VM, this tends to fail in less than 20 iterations, often in less than 10 iterations. On a 4 vCPU VM, this may not fail in the 100 iterations but I sometimes see it fail after many iterations. If you use the -Provider EXO, it never fails which is why it appears to be a bug in the Microsoft.Graph modules.
### SDK Version
ModuleType Version PreRelease Name ExportedCommands ---------- ------- ---------- ---- ---------------- Script 2.24.0 Microsoft.Graph.Authentication {Add-MgEnvironment, Connect-MgGraph, Disconnect-M… Script 2.24.0 Microsoft.Graph.Users {Get-MgUser, Get-MgUserCount, Get-MgUserCreatedOb…
### Latest version known to work for scenario above?
_No response_
### Known Workarounds
- Using more virtual CPUs seems to decrease the frequency of the issue but may not avoid it completely.
- Not allowing the object to be emitted to the pipeline seems to work around the issue. For example, if you only return one property of the object to the pipeline, e.g $MGUser.UserPrincipalName, then the error does not occur.
- Does not occur when -Debug argument is used with Get-MGUser
### Debug output
<details><summary>Click to expand log</summary>
```
### Configuration
-OS: Windows 11
-Architecture: x64
```powershell
Name Value
---- -----
PSVersion 7.4.5
PSEdition Core
GitCommitId 7.4.5
OS Microsoft Windows 10.0.22631
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
```
### Other information
_No response_
Describe the bug
Calling the Get-MgUser and other Microsoft.Graph functions often cause my scripts to fail with this error: format-default: There is no Runspace available to run scripts in this thread. You can provide one in the DefaultRunspace property of the System.Management.Automation.Runspaces.Runspace type. The script block you attempted to invoke was: …
The problem is more reproducible on virtual machines with 2 virtual CPUs. It seems this occurs when the Microsoft.Graph objects are emitted to the pipeline.
Expected behavior
Emitting Microsoft.Graph object to the pipeline (or screen) should not terminate PowerShell scripts.
How to reproduce
Create a PowerShell script called Test-format-default and paste this text:
for ($i = 0; $i -lt 100; $i++) { Write-Information -MessageData ("Iteration {0}" -f ($i + 1)) -InformationAction Continue switch ($Provider) { "Graph" { $MgUser = Get-MgUser -Filter "DisplayName eq '$User'" -Property $PropertyList | Select-Object -First 1 $MgUser } "EXO" { $EXOMailbox = Get-EXOMailbox -Filter "DisplayName -eq '$User'" $EXOMailbox } } Start-Sleep -Seconds 2 } $ProgressPreference = $origProgressPreference