How to retrieve the value of @odata.count

Ravindra Shukla 121 Reputation points
2021-05-10T15:03:31.417+00:00

Hi,

I am writing a powershell script to retrieve the count of unread emails of an user using Microsoft Graph API.
However its returning me a full response as below, however I only want to retrieve the value 281 of "@odata.count" field in the response and display as output of my powershell script.

Could you please help me with this query? How can I achieve this or whether this is possible and if yes, how to use this in powershell script?

Thank you.

> {
> "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('677887655689900098888')/messages",
> "@odata.count": 281,
> "value": [
> {
> "@odata.etag": "WAAAA\AACCCNNJHHHHFHHJF"",
> "id": "WAAAA\AACCCNNJHHHHFHHJWAAAA\AACCCNNJHHHHFHHJFWAAAA\AACCCNNJHHHHFHHJFWAAAA\AACCCNNJHHHHFHHJF=",
> "createdDateTime": "2021-05-03T14:19:22Z",
> "lastModifiedDateTime": "2021-05-03T14:19:24Z",
> "changeKey": "CQAAABYAAAAiIsqMbYjsT5e/T7KzowPTAAOIXIRc",
> "categories": [],
> "receivedDateTime": "2021-05-03T14:19:23Z",
> "sentDateTime": "2021-05-03T14:19:23Z",
> "hasAttachments": false,

Microsoft Security | Microsoft Graph
0 comments No comments

Answer accepted by question author
Ravindra Shukla 121 Reputation points
2021-05-11T16:25:53.71+00:00

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. ******@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)"  

Was this answer helpful?


2 additional answers

Sort by: Most helpful
  1. Ravindra Shukla 121 Reputation points
    2021-05-12T06:21:39.573+00:00

    Hi @Vasil Michev ,

    Its working now. Thank you for your help.

    Was this answer helpful?

    0 comments No comments

  2. Vasil Michev 128.7K Reputation points MVP Volunteer Moderator
    2021-05-10T17:11:41.05+00:00

    The full code would be helpful, but assuming you are storing the response in $resp variable, it will be something like:

    ($resp.Content | ConvertFrom-Json).'@odata.count'
    

    Was this answer helpful?

    0 comments No comments

Your answer

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