How To Read Mailbox Messages In Office365 Using Microsoft Graph API And PowerShell #microsoft

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign what's going on folks welcome to the channel uh it's been a while since I posted a new video but here I am with a new content uh today I'm going to show you how you can get total message account in a particular mailbox uh folder in a user mailbox using Microsoft graph API and partial in Microsoft 365. so first and foremost what is Microsoft graph API Microsoft graph API as you can see from that page that is from Microsoft docs it's a restifo web API that enables you to access Microsoft cloud services service resources and programmatically okay so if you're interested to access any M365 data programmatically using an API you can leverage on Microsoft graph API okay so the purpose or what you're going to focus today is to use Microsoft graph API with partial to be able to get a message statistics that is the total message count from inbox folder for an Office 365 user okay so the main HTTP method that you're going to focus with today is only the get and with subsequent videos that I will post uh in the future we will see how we can use post patch put and delete our HTTP methods so without further Ado we can begin so what we need before we start you need first to register a nap registration in Azure ad that is the app that you're going to use to access the resource the app will give us your token that we need to access the data from the backend and um we will need a module we need to install a partial module named MSR dot PS in our system so that we can be able to generate the access token okay so we can head over to ajd and do the app registration we do that let me just log on to my tenant here so you need first to log into your M365 tenant as a global administrator okay next you just need to click on the up launch on the left top left corner choose admin you can click show all so that you can be able to see all the admin centers we are interested in Azure ID so you can click Azure active directory foreign from here you need to scroll down and then you locate the app registration not Enterprise but app registrations and then from as you can see I have a few of applications that I created just for running the tests so you can create your own by just clicking on the new registration a link on the top so I'm going to call it um demo API app you can call it the name that you want you only friendly name just need it to be unique so demo API looks good for me then from here what uh can you use this application access disappear so you just need because you can go with the first option but for single tenant and a single 10 and then you hit register okay once you have your application the next thing is to configure the application so we're going to start with um I'm going to start with APA permissions so the API permission is what we need uh we need the application to give a success in entry in um in M365 so what we need to do is we just need to click on ADD permission and then you select Microsoft graph and then from here we have to as you can see we have delegated permissions and we have application permissions because you're using application to get that uh to get the tokens to access the data or you're using an application uh developed using a partial to access the data we need to go with the application permission but if you are to do a dynamic token access or you want to be able to get the access token dynamically you can go to the delegated permissions okay so I'm going with application permission and then from here you'll be able to see um all the permissions that you need to assign the application so we are only interested in reading or getting data from uh user mailbox so we're going to locate the mailbox permission so it's usually mail scroll down and locate mail here we go so you can select mail so as you can see we have uh four sets of permission that we can assign under me actually it's not voice five we have mail.read basic all we have mail.freebasic we have also made the grid you're not interested in writing or sending so you're going to go with male dot read you can go just select the three the ones with read so middle dot read the three the first three and then you can click add permissions so once you add permission you need to click Grant admin consent for uh for your tenant so Bernard comes that's the name of my channel so you just need to hit that and choose yes to plan the consent so the permission is already assigned the next thing that you're going to configure is the Authentication okay so for the authentication you just need to select add platform and then you need to scroll down for hours is from Attack it's running from a desktop application so we just need to select mobile and desktop application from here you need to choose the first option okay and here is is this is just optional but you can go and type like https that's the redirect URL and then you can hit localhost just like that I think it should be just https or http whatever and then you can choose configure that is an optional thing that you can do okay just scroll down starting in there since something looks funny okay once you you add you configure the mobile desktop application to appear under the platforms that the up your um your application with authenticate through so it's already configured there we only using uh mobile and desktop application with this application with the other applications you can call it that way okay so the next thing is um the certificates and secrets so we need a secret for application so just go and eat new client secrets can give it um statistics the expiration date so from my case since this is just a test we can go with they just recommended six months so once I do the video just uh get rid of the application so you can give it like a description name so my great Secret and then you hit add so tricky one here you just need to copy this uh the value of your secret before you get out of um you get out of this page because once you refresh the page this value will not uh appear so you will be forced to create another secret and then you grab this uh secret value so I just need to copy my secret value and paste it in a notepad so instead of just adding a notepad I can open Visual Studio code here and then add secret Dot txt and then I can add secret there actually we need that value just to demonstrate what happens if I refresh this page that secret will disappear you can see we only have the first three characters of our secret and then from there we cannot be able to get it so it's the only way you can get around this is to create another secret and also if you want you can delete this one okay that's the most important that's the most important and um another thing that we need from our application you just need to go back to overview so we are done with API permission we are done with the secret to do so we are done with authentication so the next thing that you need to pull is your application client ID can go back to visual studio code and then just add it somewhere app client ID then press it there the next thing we need is the tenant ID so you'll notice I've not copied the object ID it's the application client ID okay so next is deterrent ID go back to visual studio code and just just attendant ID just like that so that's what we need to be able to get the access token to access those resources using Microsoft graph API so the next thing that you need to install in your system is the micros and not Microsoft the MSR dot PS module for getting that access token it's here so how you install it just scroll down is the installation script just copy that and then you open partial you can launch partial this administrator and then you paste you paste it here I had already done this so Ctrl V why isn't not working sorry okay there we go so that's what you need to do and then you hit enter enter I had already done this so you need to install the amazon.ps module once that is done we can go back now to the scripting so I'm using visual studio code it's my code editor for this tutorial so I'm just going to create a new description call it Main that would be as one just like that and then now we can begin coding okay so I can just get rid of the terminal there once I create the script I'll just paste it in partial directly there okay so let me just open my mailbox to show you Outlook so there we go I got a couple of messages in my inbox so you're only going to get the message found in this inbox or the emails that are inside the inbox you can also graph through that send part for this tutorial we're going to focus it's the same thing or we just need to do is from there are request endpoint we just need to reference the particular mailbox folder that we need to get the report from so you can go back to our scripting so so once you're done with the installation of the module that is required the MS cell.ps for getting the access token I'll also leave uh the link to that module uh in the video description so that you can go just directly and grab and the installation script or you can even uh Fork it to your GitHub repository if you have one for reference okay so the first thing is to write the snippet that will allow us to generate uh the token using the MSL uh the PS module there it goes just now go dollar sign so that's how you create an object in partial I'm going to call it connection details it will add and then calibrations there so connection details that is in order so the first thing we need to add is our application ID so add a comment there so application client ID so for mine is here so just copy that that is for the application that we had created so the variable for that you just need to add single quartz and then just type client ID and then two quotes double quotes you paste your client ID there okay so this is not supposed to be full column it's supposed to be equal I was wondering where I'm getting that from errors there so the next is a tenant ID doesn't matter with the order and then my tenant ID is here copy that I press there we can add a comment here then on ID next is now the secret of Secrets this one client secrets just like that then from there we're going to paste our secret value there with that then press okay one more thing with app secrets you need to convert it into a secure uh string so the way you do that you just need to add the pipe uh pipe simple and then you can call convert to secure should be convert to secure um why am I not getting the suggestion or you can type it convert to secure string and then from there we need to call in s plane as plain texts and force okay so we'll have it in a secure way so so there are a couple of also of ways that I needed to discuss with you that you can test Microsoft graph API you can use Postman tool it's a tool that you can use to test graph API like Microsoft one here you can also use the Microsoft built-in tool let me show you where it is so I like to use the Microsoft graph APA developer tool and to such that it will be the first one to appear in the page you can click try and then there we go you need to sign in but since in this session I'm signed in using the global admin of my account so that should reference directly let me see if we start through it's not so I just you need to click sign in from here sign in and then you can grab the account that you want to sign in and then now from here you can have a very clean visualization of the request that you make to Microsoft's graph API so here it's me me means the current logged in or the current session user so if you want to change to another user you just need to remove me and then now you can reference the UPN or the email address of the particular user that you need to query via information from there okay so once we query here we can be able to get even the access token how it looks like so for example if I want to just click me there and then I hit run query you can see I have a response status is 200 okay and then this the data so I'm getting that my display name given name information about my account the profile and then when I scroll up and then go to access token there we go I have the access token that I need that's allowed me to access that information okay so with msl.ps that's what we are doing from this script so this one will allow us to be able to query uh the access token and then we can pass in the access token to our request uh endpoint or a request code okay so the next thing that you need to do is to create uh an object I can call it token and then here now I can call a method from msl.ps called MSO token and then I can pass in on my connection details should be connection details just like that so if I want to get the token now the one that will be returned because this will return a lot of information I'm not I'm just only interested with the token only if I were to run this code let me just show you what you get interest rate token there and then copy the entire thing then go to partial and paste the term convert to this incorrect tool I've noticed where the issue is it's on the spelling convert convert convert to secure so that's what it's supposed to look like so Ctrl C go back to partial best okay also getting an error just hold on guys we're missing something there again missing something okay guys these are some of the issues that we usually face so okay we need to do here a position by um okay so we can just go back I believe we grab the correct application ID just drop it again tenant ID client secrets client ID is the sum close to the screen again just grab that okay guys it just went through I think there was an issue with the client ID and so let's see if we can be able to get anything token there we go okay so I think there was an issue with um how I tested the client ID should be zero let me go back to this strong this one but we are good to go now so we have a test talking here so this is the token that will give us access to the mail uh results Okay so if you want just to grab the access token only we can put it in a different um uh object so that we have specifically that token instead of all these uh returned uh data so go back to partial and then now from there we can create another object and call it access token and then we can pass in Access token told this one now access token there you can see we also have that automatic uh suggestion so dot access token so from there now we can use this access token and pass it to uh request endpoint that you're just going to construct now in a few so now since we have the access token we can proceed with now creating the the the request endpoint so I can just add a comment here and add access token d or just access token just like that okay next we are only interested to grab um the total message count in the inbox folder so the way you construct the end point I can create a new object and call it graph URI or you can just add like mail let me just go with the graph API okay this need to be in double quotes and https so just like I showed you back to graph Explorer tool so it's this time you're going to use this for version we're going to use V 1.0 so we have a couple we have two that is we have better better is under preview so Microsoft only recommends to use in production the one that is already uh official release so this one is still in preview so we cannot use it in production environment but for testing we can work on it also we can also utilize it uh only that if something fails you might need to come back and do uh troubleshooting that's when Microsoft has done an update on the beta and you need to also uh update your application again so we can go with v 1.0 that is version so go back so https graph dot Microsoft oh comb and then slash V point zero V point one zero that is the version of the cloud API and then slash users and then now slash we need to reference the email account that we need or the UPN of the account that we want to get the mailbox information so foreign object and call it mail user and then I can pass in now my email address Banner add one comes dot on Microsoft .com like that and then here I can just reference the variable mail user just like that so I'm accessing under users I'm only interested with my account so as we progress with the tutorial I will also show you how we can get um data for multiple users so we will just need to construct a CSV file or a TXU file and then we read we append all the email addresses for the users that we want to read their data and then we can grab that we do a loop and we can pass in the loop variable or object to our request endpoint so that we can grab data for multiple users so next uh query will be mailbox should not be mailbox but mail folder mail folders and then we go with inbox messages and then please messages so as you can see there we have the reference inbox folder go back to my account my mailbox so the inbox so if you want to grab uh the total account for drafts we just need to edit uh our endpoint and just reference here draft and so forth so inbox and then messages and we need to add the question mark simple and then now our query parameter there are a couple of query parameters that you can use uh with graph API we have uh you have to skip we have the filter we have order by we have we have a couple so if I can just go back to the documentation just show you some query parameters that are available for use okay just here query parameters so there we go so you have count expand filter formats in our case we're going to work with the skip and um the account to grab the total count of those messages so back you need to Escape nothing and then the Amplified then count equal to true okay that looks good so that's a request uh endpoint the way it will look like an axis to now do the requests to our graph API so just to send the request so the way you do that you can call it you can create another variable or an object and call it response and then from there we need to call invoke response invoke you should have a suggestion in Block rest method it's not there yeah they are invoke rest method so the method that you're using is get parameter for URI here we're going to pass in now the URL so I forgot to add graph URI so graph you are right there that's how you arrive next is parameter for content type this should be application slash slash or Json needed to communication format then we have headers for the headers now we're going to pass in our authorization access token so you construct that you need to add the add simple and then the Cali braces add authorization authorization is equal to error let me just wrap it one drop okay around foreign and then now we have should be out dollar and then access token they're at the top so access token only you're going to pass it here okay so it's a bear so that's now our request um code so if we can run this code and just to see what you're going to get so I can add response just at the end and then I can just pull everything go back to partial clear the screen and press that board give it some time to run and enter there we go and as you can see we have a return data of all data dot count of 208 so this is the total count of messages in the inbox uh folder if you can use Microsoft's graph Explorer tool and we just referenced the Samsung request endpoint that is in our partial so if I can go here and then grab this up to there and then me me will mean the current login user or session user and then press start and then run the request you can see 200 okay and then now we also have the audit account is 208 so 208 is the total messages in my inbox uh folder so if we are interested in another folder like for example drafts should be draw no not wrong you just confirmed from my mailbox yeah drafts and then I run that request there we go only one message and we can confirm just was attain if that is true and under drafts we have only one message under send we have a couple of messages not a lot but also we can add send items and then see what you get so it should be send items you should not have space there there we go and we have a total messages of 164 in our sin uh folder so that's all guys you can do so if you want to do like the entire statistics of all uh mailbox folders for a particular user we just need to write uh from the partial you just need to construct more uh request endpoints referencing the uh the folder that you need the mail folder so if for example you are to grab for drafts together with inbox I just need to come here and construct another endpoint and for the for the reference drafts and for the response also when we're doing the request you need to also perform the request for that particular uh endpoint independently okay so now since we have the response we are only interested in dot count or data dot count the way you do that as you can see from the result we you're not interested in this value we're only interested with.com so you want to get only the.com so you just need to test that to reference that directly just like that and then when we go back to and we run this code and see what we have get a mirror okay supposed to be directly it's supposed to be or data or data.com just throw them let me just grab it without just a response thenresponse.com fell foreign foreign foreign but when we are passing this information directly to partial uh not sorry not too partial but to uh our report that is through a CSP file we will be able to pass in it directly we'll pass it directly and it will give us uh the response so for example if you wanted to now uh add or get that information in a CSV file you need to construct one so you need to create a folder so if you want to send this information for this user or that report to a CSV file we will need to create a folder in our system so that we can store that file or the CSV file so the way you do that um just need to create new and then call it report s and then equal here I'm going to reference see I've created a folder in C drive and temp and then the name of the file that you're going to write to it's going to be report dot c s v so if I can go back to Temp or to the system folder where we have the term template small letters you can see it is empty but when we run our scripts it will create that file automatically so it's a stable small letters let's skip that way so since now we have a response here we can now send that information to this CSV file for this user the way you do that is simple with partial let me just get rid of the variable and then now I can add a common and send report generate report into a CSP file so I'm going to use this format you can use the format that you are familiar with uh when you are writing to CSV file using partial for my case I like to use this format so I create a new object call it new object the name is PS object followed by record again now I'll just pipe it and add member not property that's name and then here I can call it display name sorry let me just add the UPN name for the user because I'm using the currently I'm using the email address or the UPN of the user so just UPN you just go with the up and I user principle name then we have value parameter and then here we can set it now to mail user mail is next uh is now the total account or email so it's the same thing so I can just copy paste this instead of just retyping it again so past and then do some field editing so here I need to write inbox total message count like that and what I'm going to pass in there is response code um response code and um I think I've figured why we we could not get the all data account using our response object so if I can comment that quickly just to demonstrate something that I was missing so if I need to grab the order the total account you just need to add response Dot and then in single quotes then from there I can pass in the all data so this is what I'm interested in so I can pass in that just like that and then we have it so if I copy that and uh clear this screen and then press that only you have now the click it that's what you needed so go back to our script I can commend that and uncomment my script and then here I can just copy paste what we have there and then paste it there so by that we have the total value for our total inbox passed in without a column next now is to explore that information to the CSV it's simple just call record again for example export CSV then ola export or just need to be report s this is this which is the reports path let me just add it path to make sense path and then no type with a pen switch okay there we go so now we can I can just copy the entire script and paste it in partial and then we see it will get that file generated so Ctrl C and if I come back here as I told you in our temp is empty currently so after running the script we expect to have that CSP file created with the data so paste your script there press enter and see everything went well and then we can go back to our temp uh or temporal folder and there we have the file reports you can open it to see what we have inside and uh guys that we we have a CSV with two columns first is the user principle name and we have the inbox.com message account and the value is 288 so we can see that our script is working and what we have implemented is working properly so simply guys that's how you can be able to read any type of resource in Microsoft graph API using uh Powershell so it applies the same if you want to read groups if you want to read users if you want to read um Drive data from SharePoint or OneDrive anything that you need from Microsoft graph API and you just need to read all the gets that's how simple you can do it and with these particular script so today we are only focusing on the mailbox uh folders in the subsequent videos that I will make as we progress we will see how we can uh posts in the inbox uh sorry how we can uh posters in the inbox true and uh we will see how we can uh send from the inbox using the application go back overview how we can be able to also delete messages from a particular mailbox folder so that is in subsequent videos to come so guys if you love the content you can feel free to subscribe for this type of um videos that I will be making uh as we progress so we are not limited just to emphasize we are not limited to using a partial I use partial because for my case I see it simple to do it um with you can do it with python you can do this with C sharp you can do with go programming language you can do it PHP Java yeah those types of languages are supported with Microsoft graph API you can also uh do your own test them okay so I just showed you how we can be able to grab for a single user what about if we needed to grab for multiple users like two users or three users more than one user that is so there we want we cannot go directly and just reference directory like this and then for example you add another user there no there is a technical way that we can implement this the way you do it uh is simple so we just need to create a new file here again a txt file actually you can use a CSV if you want so I'm going with a txt file and call it users and then from there I can just add a Samsung email address for this user and another one from my Office 365 users let me just go back and grab another user it's licensed and with content so I just need to go with this one so I can add email address for that to see the total message count in the inbox for that user the other is the Samsung copy paste then I can save that txt see users and then we can okay let me just get to that one I can also delete this so that we regenerate it again so there we have two users in our txt file so create your txd file add your users there you can add the number of users that you want and then you can go back to a script to modify it so the way you do it first we need to reference our users but so I just what I'm going to do is users and then it's in C drive in temp folder slash uses or txt so in partial how we get content from a txt file is just calling the get monthly get content actually we can also pass in path is an optional uh parameter so get content path there and then now we can Loop so let me just rename this and call it mail users like that and then now from here I can do a for Loop so for each dollar mail user in mail users not so our for Loop will look like so for each male user in mail users I can get rid of this and copy this chord and paste it inside there the the for Loop or inside the loop okay so that way so for each nail user email users or users in our users list here we need to reference each user there and the other code just Remains the Same so I can copy these now Ctrl C go back to partial clear the screen this guys told you that we have only the users list are two users so if I go to partial and press the script there e and run it just completed successfully no issues and go back and there we have our reports file generated and we have uh data for our two users there so we have one for Barnard and also for Banner user there so Fort user 40 emails are inside the inbox folder for this particular mailbox and this one we had already seen that okay so guys I hope you loved that so that's how simple we can do this thing and uh in subsequent videos that I'll make we will see how we can grab uh other type of resources using graph API and technically uh specializing in a passion scripture okay that's it guys for today I have I hope you learned something uh if you face any issues uh you can feel free to post in the comments uh uh in the in the comments and I will be happy to assist you guys so happy scripting uh days for you guys okay bye bye
Info
Channel: Bernard Mwanza
Views: 11,764
Rating: undefined out of 5
Keywords:
Id: zIwfTsAKnUg
Channel Id: undefined
Length: 63min 57sec (3837 seconds)
Published: Sat Sep 24 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.