Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"format-default: There is no Runspace available to run scripts in this thread" is fatally terminating scripts #3009

Open
SiloReed opened this issue Oct 24, 2024 · 0 comments

Comments

@SiloReed
Copy link

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

  1. Deploy a Hyper-V VM running Windows 11 with 2 vCPU and 4 GB RAM
  2. Install PowerShell 7 with: winget install --id Microsoft.PowerShell --source winget
  3. Start PowerShell 7 "as Administrator"
  4. Install Microsoft Graph: Install-Module Microsoft.Graph -Scope AllUsers -Repository PSGallery -Force
  5. Install Exchange Online PowerShell: Install-Module -Name ExchangeOnlineManagement -Scope AllUsers
  6. Create a PowerShell script called Test-format-default and paste this text:
[CmdletBinding()]
param(
    [Parameter(	Position=0, 
    Mandatory=$true,
    ValueFromPipeline=$False,
    ValueFromPipelineByPropertyName=$False) ]
    [string]
    $User,

    [Parameter(	Position=1, 
    Mandatory=$true,
    ValueFromPipeline=$False,
    ValueFromPipelineByPropertyName=$False) ]
    [ValidateSet("Graph", "EXO")]
    [string]
    $Provider
)
$origProgressPreference = $ProgressPreference
$ProgressPreference = 'SilentlyContinue'

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
  1. Use Connect-MGGraph to connect.
  2. 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

Click to expand log ```
</details>


### 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

@SiloReed SiloReed added status:waiting-for-triage An issue that is yet to be reviewed or assigned type:bug A broken experience labels Oct 24, 2024
@timayabi2020 timayabi2020 added Status: Needs Investigation and removed status:waiting-for-triage An issue that is yet to be reviewed or assigned type:bug A broken experience labels Nov 21, 2024
@timayabi2020 timayabi2020 self-assigned this Nov 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants