Forum Discussion

heinzelrumpel's avatar
heinzelrumpel
Copper Contributor
Mar 28, 2025

MGraph suddenly stops working

PS C:\Windows> Get-MGUser -All
Get-MGUser : InteractiveBrowserCredential authentication failed: 
In Zeile:1 Zeichen:1
+ Get-MGUser -All
+ ~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-MgUser_List], AuthenticationFailedException
    + FullyQualifiedErrorId : Microsoft.Graph.PowerShell.Cmdlets.GetMgUser_List

 

Prior to this I did a "connect-mgraph -Scopes "User.Read.All" " and authenticated myself with MFA. Did not get an error doing so. Logged in as a global administrator.

 

 

Any ideas what i going wrong? I know, the error indicates Authentication Failure, but Authentication looks correct

 

4 Replies

  • jimvh's avatar
    jimvh
    Copper Contributor

    Hi Harm,
    I had some automation scripts running (for longer periods) where i had the same issue.
    The script starts, after a while i get an error (authentication failure)

    It seems that in Graph there's an expiry on an connection.
    So after some time it will disconnect and you have to reconnect again.

    I made a module built in my automations (connect graph with an app registration)
    Then it checks each minute if the expiry will take place, if it will then it automaticly reconnects:


    On top of the script:


    function ConnectGraph {
        # Populate with the App Registration details and Tenant ID
        $appid = 'Your App-ID'
        $tenantid = 'Your-Tentant-ID'
        $secret = 'Your-Created-Secret'

        $body = @{
            Grant_Type    = "client_credentials"
            Scope         = "https://graph.microsoft.com/.default"
            Client_Id     = $appid
            Client_Secret = $secret
        }

        $connection = Invoke-RestMethod `
            -Uri https://login.microsoftonline.com/$tenantid/oauth2/v2.0/token `
            -Method POST `
            -Body $body

        $global:token = $connection.access_token
        $global:secureToken = ConvertTo-SecureString $global:token -AsPlainText -Force
        $global:expiresIn = $connection.expires_in
        $global:lastConnectTime = Get-Date

        Connect-MgGraph -AccessToken $global:secureToken
    }

     

    function EnsureGraphConnection {
        if ((Get-Date) -gt $global:lastConnectTime.AddSeconds($global:expiresIn - 60)) {
            Write-Host "Refreshing MSGraph connection..." -ForegroundColor Yellow
            connectgraph
        }
    }

     

    Then before running your code, kick of the function connectgraph, and set variable of $lastconnectiontime

    connectgraph

    Now your connected to MSGraph.
    Build a loop (while) and put this part on top of that foreach loop:

       $success = $false
                    while (-not $success) {
                        try {
                            EnsureGraphConnection
                           ###Your code here###
                            $success = $true
                        } catch {
                            if ($_.Exception.Message -match "token is expired") {
                                Write-Host "Token expired. Reconnecting to MSGraph..." -ForegroundColor Yellow
                                EnsureGraphConnection
                            } else {
                                Write-Host "Something went wrong, retrying in 5 seconds..." -ForegroundColor Red
                                Start-Sleep -Seconds 5
                            }
                        }
                    }

    after the else block you can also put "continue" to skip the current item

     

     

     

     

     

     

  • I had the same issue when collaborating with someone on a project. Graph 2.26.0 had problems, and 2.25.0 was better. "

    Uninstall-module -name "Microsoft.Graph.Authentication
    Install-Module -Name Microsoft.Graph.Authentication -Scope CurrentUser -MaximumVersion 2.25.0

  • LainRobertson's avatar
    LainRobertson
    Silver Contributor

    Hi heinzelrumpel,

     

    From your description, it doesn't sound like you've done anything wrong.

     

    You could check the Application log in Event Viewer to see if there's a more detailed breakdown of the error(s).

     

    I'd also check that you do not have multiple differing versions of the same modules installed - which can happen if you only every run Install-Module and Updates-Module.

     

    You can check using:

     

    Get-Module -ListAvailable -Name "Microsoft.Graph.*" | Select-Object -Property Scope, Version, Name | Sort-Object -Property Name, Version;

     

    If you find you do have duplicate modules, you will want to remove the older version. Or, you could just remove them all and then reinstall the ones you need.

     

    But this is just a generic tip since I haven't seen that error before nor can I reproduce it using the module versions I am currently using.

     

    Cheers,

    Lain

Resources