Hi @Vasil Michev ,
Thank you for your reply.
I tried the one which you suggested, however I am getting an error as "ConvertFrom-Json : Cannot bind argument to parameter 'InputObject' because it is null."
Am I doing something wrong here?
My full code for the reference as below.
==================================================
$tenant = Read-Host ("Enter your tenant name")
$userUPN = Read-Host ("Enter your upn id, E.g. userid@domain.com")
$openid = Invoke-RestMethod -uri "https://login.microsoftonline.com/$tenant/v2.0/.well-known/openid-configuration"
Write-Host "The token endpoint of your directory is"
$openid.token_endpoint
$token = $openid.token_endpoint
$Body = @{
client_id = "XXXXXXXXXXXXXXX"
client_secret = "XXXXXXXXXXXX"
redirect_uri = "https://localhost:4000"
grant_type = "client_credentials"
scope = "https://graph.microsoft.com/.default"
tenant = $tenant
}
Write-Host "Requesting access token"
$request = Invoke-RestMethod -uri $token -Body $Body -Method Post
$request.access_token
Write-Host "Your upn id is $userUPN"
$graph_api = "https://graph.microsoft.com/v1.0/users/$($userUPN)/messages?`$filter = isRead ne true & `$count = true"
$graph_api
$resp = Invoke-RestMethod -Method Get -Uri $graph_api -ContentType "application/json" -Headers @{Authorization = "Bearer $($request.access_token)"}
$resp1 = ($resp.Content | ConvertFrom-Json).'@odata.count'
Write-Host "The count of unread emails of the given upn id is `$($resp1)"