We are creating a scheduled task with powershell script to check for group licensing errors in Azure/Entra ID using Graph API but the connection throws an error.
`# Connect to Graph
$token = $Context.CloudServices.GetAzureAuthAccessToken("https://graph.microsoft.com")
# $token = $Context.CloudServices.GetAzureAuthAccessToken()
$token = $token | ConvertTo-SecureString -AsPlainText -Force
Connect-MgGraph -AccessToken $token
$users = Get-MgUser -All -Property AssignedLicenses, LicenseAssignmentStates, DisplayName | Select-Object DisplayName, AssignedLicenses -ExpandProperty LicenseAssignmentStates | Select-Object DisplayName, AssignedByGroup, State, Error, SkuId
#count the number of users found with errors
$count = 0
# Loop through each user and check the Error property for None value
foreach($user in $users) {
if($user.Error -ne "None") {
$count += 1
Write-Host "User $($user.DisplayName) has a license error"
}
}
if ($count -le 0) {
write-host "No user found with license errors"
}`