Dear pemmasaniphaneendra pavan sai,
Welcome to community!
Thanks for sharing your PowerShell 7.2 runbook setup for connecting to Microsoft Graph using a service principal (SPN). Based on the error you're encountering "Invalid JWT access token" and according to my research, here are some explanations and recommended fix.
This is a known compatibility problem betweenbPowerShell 7.2 runtime in Azure Automation and Microsoft.Graph PowerShell modules version 2.26.1 or later. These versions introduce changes in how JWT tokens are handled, which causes the Connect-MgGraph command to fail with the "Invalid JWT access token" error, even when credentials and permissions are correctly configured.
Option 1: Downgrade Microsoft.Graph Modules to Version 2.25.0
I have one Azure Automation account where Microsoft Graph modules installed with 2.26.1 version:
When I ran your script in PowerShell 7.2 runbook, I too got same error as below:
To resolve this, you need to revert back its previous version 2.25.0 as a workaround.
In my case, I deleted the existing 2.26.1 Microsoft Graph modules as below:
Now, I ran below PowerShell script in Azure Cloud Shell to install2.25.0version Microsoft Graph modules:
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.Users module
$moduleName = 'Microsoft.Graph.Users'
$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'
Response:
Make sure to wait for few minutes until the modules status turns "Available" as below:
When I ran the PowerShell script again after reverting back to2.25.0version, I got the response successfully as below:
Define App Registration details
$tenantId = "tenantId" $clientId = "appId"
$clientSecret = "secret" # Client secret value
Define the resource for Azure Management API
$scope = "https://graph.microsoft.com/.default"
$tokenUrl = "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token"
Get authentication token using client credentials flow
$body = @{
grant_type = "client_credentials"
client_id = $clientId
client_secret = $clientSecret
scope = $scope
}
try {
$response = Invoke-RestMethod -Method Post -Uri $tokenUrl -ContentType "application/x-www-form-urlencoded" -Body $body
$accessToken = $response.access_token
Validate token retrieval
if (-not $accessToken) {
Write-Host "Failed to obtain access token" -ForegroundColor Red
exit
}
Write-Host "Access Token obtained successfully" -ForegroundColor Green
Convert the access token to a SecureString
$secureAccessToken = ConvertTo-SecureString $accessToken -AsPlainText -Force
Connect to Microsoft Graph using the SecureString access token
Connect-MgGraph -AccessToken $secureAccessToken
Write-Host "Connected to Microsoft Graph successfully" -ForegroundColor Green
}
catch {
Write-Host "Error: $_" -ForegroundColor Red
}
Import-Module Microsoft.Graph.Users
Get-MgUser -Top 10 | Select-Object DisplayName, Id
Response:
To know more regarding this known issue, here are my reference: Invalid JWT access token - Microsoft Q&A
Additional Tips
- Ensure your SPN has the correct permissions (e.g., User.Read.All, Group.ReadWrite.All) with admin consent.
- If using ClientSecretCredential, make sure you're not passing a SecureString directly—this is a common mistake. Instead, use Connect-MgGraph -ClientId -TenantId -ClientSecret directly or use Invoke-RestMethod to fetch the token manually if needed
I hope this information is helpful. Please follow these steps and let me know the outcome.
***Note:***Please understand that our initial response does not always resolve the issue immediately. However, with your help and more detailed information, we can work together to find a solution.
If you have any difficulties when trying these methods or the issue still persists after completing the above, feel free to reach out, and we can further investigate the problem together.
Thanks for your patience and understanding so far. Looking forward to hearing from you.
Best Regards,
Vivian - MSFT | Microsoft Community Support Specialist
*If my answer is helpful,please mark it as an answer, which*****will definitely help other community members with similar questions find a solution to their problem faster.