question

RavindraShukla-2370 avatar image
0 Votes"
RavindraShukla-2370 asked saldana-msft edited

How to retrieve the value of @odata.count

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-mailmicrosoft-graph-users
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RavindraShukla-2370 avatar image
0 Votes"
RavindraShukla-2370 answered michev commented

Hi @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)"




· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Right, my example was for Invoke-WebRequest. When using Invoke-RestMethod, you're already getting the parsed JSON. So all you need is:

 $resp.'@odata.count'
0 Votes 0 ·
michev avatar image
0 Votes"
michev answered

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'
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RavindraShukla-2370 avatar image
0 Votes"
RavindraShukla-2370 answered

Hi @michev,

Its working now. Thank you for your help.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.