microsoftgraph / msgraph-sdk-powershell

Powershell SDK for Microsoft Graph
https://www.powershellgallery.com/packages/Microsoft.Graph
Other
712 stars 172 forks source link

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

Open SiloReed opened 3 weeks ago

SiloReed commented 3 weeks ago

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

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_