Home > other >  Read O365 messages via powershell (0365 API)
Read O365 messages via powershell (0365 API)

Time:12-09

I need to read the O365 messages, find the right subject and body and print log message, using 0365 API.

Im connect to o365 api, get token and messages.

$connectionString = @{
'cliendid' = ""
'tenantid' = ""
'clientsecret' = "" convertto-securestring -asplaintext -froce
}

$token = Get-MsalToken @connectionString
$accestoken = $token.AccesToken

$mailuser = "[email protected]"
$graphapi = "https://graph.microsoft.com/v1.0/users/$mailuser/mailfolders/Inbox/messages"

$responce = Invoke-RestMethod -Method get -Uri $graphapi -ConectType "application/json" -Headers @{Autorization=("bearer {0}" -f $accestoken)}

And how do I read the response json and find needed subject now ? Thank you very much!

CodePudding user response:

List messages - Microsoft Graph

The MS Graph API Documentation will show an example of a response to your API call.

A call like this should return an array of message objects in your "responce" variable. I believe if you express and look in $responce.Content[0] it will contain your first message. So, according to API Docs, $responce.Content[0].Subject should be your first subject. If you run...

$responce.Content | Out-GridView

You should get a grid view listing all properties of your response content. It's been a little while since I've used Graph API so the "drill down" to the content array may be a bit different. Each element of the content array should be in a format like below (From Graph API docs).

{
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('bb8775a4-4d8c-42cf-a1d4-4d58c2bb668f')/messages(sender,subject)",
    "value": [
        {
            "@odata.etag": "W/\"CQAAABYAAADHcgC8Hl9tRZ/hc1wEUs1TAAAwR4Hg\"",
            "id": "AAMkAGUAAAwTW09AAA=",
            "subject": "You have late tasks!",
            "sender": {
                "emailAddress": {
                    "name": "Microsoft Planner",
                    "address": "[email protected]"
                }
            }
        }
    ]
}
  • Related