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.
Also, make sure the Automation account’s managed identity has the Application.Read.All application permission with admin consent.
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
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.
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.