How to retrieve the value of @odata.count

Ravindra Shukla 116 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 Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,715 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ravindra Shukla 116 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. 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)"  
    

2 additional answers

Sort by: Most helpful
  1. Vasil Michev 95,836 Reputation points MVP
    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'
    
    0 comments No comments

  2. Ravindra Shukla 116 Reputation points
    2021-05-12T06:21:39.573+00:00

    Hi @Vasil Michev ,

    Its working now. Thank you for your help.

    0 comments No comments