Getting failure with Connect-MgGraph.

Anil Kumar Saripadi 40 Reputation points
2025-05-22T20:57:29.8833333+00:00

Get-MgApplication_List: Line | 5 | $appList = Get-MgApplication -All | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | Authentication needed. Please call Connect-MgGraph.

Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,366 questions
{count} votes

Accepted answer
  1. SrideviM 5,630 Reputation points Microsoft External Staff Moderator
    2025-05-30T07:28:16.04+00:00

    Hello Anil Kumar Saripadi,

    I understand you are getting the "Authentication needed. Please call Connect-MgGraph" error when connecting to Microsoft Graph in your Azure Automation runbook using Connect-MgGraph -Identity.

    This is a known issue that arises when using newer versions of the Microsoft Graph PowerShell modules with PowerShell 7.2.

    To resolve this, remove the existing Microsoft Graph modules and install version 2.25.0 as a workaround.

    You can run the following PowerShell commands in Azure Cloud Shell to install version 2.25.0:

    # Import Microsoft.Graph.Authentication module
    
    $moduleName = 'Microsoft.Graph.Authentication'
    $moduleVersion = '2.25.0'
    New-AzAutomationModule -AutomationAccountName 'autaccname' -ResourceGroupName 'rgname' -Name $moduleName -ContentLinkUri "https://www.powershellgallery.com/api/v2/package/$moduleName/$moduleVersion" -RuntimeVersion '7.2'
    
    # Import Microsoft.Graph.Applications module
    
    $moduleName = 'Microsoft.Graph.Applications'
    $moduleVersion = '2.25.0'
    New-AzAutomationModule -AutomationAccountName 'autaccname' -ResourceGroupName 'rgname' -Name $moduleName -ContentLinkUri "https://www.powershellgallery.com/api/v2/package/$moduleName/$moduleVersion" -RuntimeVersion '7.2'
    
    

    Wait until both modules show Available in the portal before testing your runbook.

    User's image

    Also, make sure the Automation account’s managed identity has the Application.Read.All application permission with admin consent.

    User's image

    After that, you can run the following script to retrieve expired or soon-to-expire app secrets:

    Connect-MgGraph -Identity
    $appList = Get-MgApplication -All
    $results = @()
    
    foreach ($app in $appList) {
        foreach ($secret in $app.PasswordCredentials) {
            $daysToExpire = ($secret.EndDateTime - (Get-Date)).Days
            if ($daysToExpire -lt 30) {
                $results += [PSCustomObject]@{
                    AppDisplayName = $app.DisplayName
                    AppId          = $app.AppId
                    SecretStart    = $secret.StartDateTime
                    SecretEnd      = $secret.EndDateTime
                    DaysToExpire   = $daysToExpire
                    Status         = if ($daysToExpire -lt 0) { "Expired" } else { "Expiring Soon" }
                }
            }
        }
    }
    
    $results | Sort-Object DaysToExpire | Format-Table -AutoSize
    

    User's image

    For more details on this issue, refer this related thread that I previously answered: Invalid JWT access token – Microsoft Q&A

    Hope this helps!


    If this answers your query, do click Accept Answer and Yes for was this answer helpful, which may help members with similar questions.

    User's image

    If you have any other questions or are still experiencing issues, feel free to ask in the "comments" section, and I'd be happy to help.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.