Skip to content

Connect-MgGraph: try - catch not consistent #3583

@i5513

Description

@i5513

See #3582

That is a feature request about timeout option in Connect-MgGraph.

But I think the current behaviour should be fixed or documented

date; try {
      Connect-MgGraph -ErrorAction stop -ContextScope Process -TenantId 'xxx' -ClientId 'yyy' }  catch {
       Write-Warning 'Cannot Connect'
} finally { 'Esto sí se ejecuta!' }

lunes, 20 de abril de 2026 12:32:47
ADVERTENCIA: Note: Sign in by Web Account Manager (WAM) is enabled by default on Windows. If using an embedded
terminal, the interactive browser window may be hidden behind other windows.

[proceso terminado con el código 1 (0x00000001)]
Ahora puede cerrar este terminal con Ctrl+D o presionar Entrar para reiniciar.
Windows PowerShell
Copyright (C) Microsoft Corporation. Todos los derechos reservados.

Instale la versión más reciente de PowerShell para obtener nuevas características y mejoras. https://aka.ms/PSWindows

PS C:\Users\41gu0026 > date

lunes, 20 de abril de 2026 12:40:39

Expected behavior

try { connect-mggraph ... } catch { xxxx } finally { yyy } 

How to reproduce

  1. try { Connect-MgGraph -ErrorAction stop -ContextScope Process -TenantId 'a22f907a-53a6-449f-b082-22c03676d7fb' -ClientId 'e0da3fd6-9b9a-4c5e-b7ce-373f2a70b54f' } catch { Write-Warning 'Cannot Connect' }
  2. Wait 7 minutes

SDK Version

2.35.1

Latest version known to work for scenario above?

No response

Known Workarounds

I tried

Start-Job { $now = Get-Date; sleep 120; Get-Process  Microsoft.AAD.BrokerPlugin -ErrorAction SilentlyContinue |? StartTime -gt $now | Stop-Process }
 try { Connect-MgGraph -ErrorAction stop -ContextScope Process -TenantId 'a22f907a-53a6-449f-b082-22c03676d7fb' -ClientId 'e0da3fd6-9b9a-4c5e-b7ce-373f2a70b54f' }  catch { Write-Warning 'Cannot Connect' }

But It didn't work

Debug output

No response

Configuration

Windows 11
Name Value


PSVersion 5.1.22621.6931
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.22621.6931
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    status:waiting-for-triageAn issue that is yet to be reviewed or assignedtype:bugA broken experience

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions