Scenario – You need to pull a list of folders in a mailbox.
Prerequisites: You already have the following configured:
–Registered App in Azure: An Azure Registered app to connect to with Application Permissions for Mail.Read, Mail.ReadWrite, Mail.ReadBasic.All, and Mail.Send. (Not all permissions listed may be necessary for the specific function below)
–Bearer Authentication Token: A method for pulling back a bearer token and storing it to pass Authorization into your RestAPI package (See my previous post about get-accesstoken). The get-accesstoken authenticates against your Registered App in Azure.
PowerShell Scriptlets:
#Examples are below for common Folders and how to build the URI
#Notes: For common folders, you can use the name of the folder. For user created folders, or other non-common folders, you can use the Base64 ID
#1. Define $MBX
$mbx = "steve@steveman.com"
#2. Build your #APPURI
$appuri = "https://graph.microsoft.com/v1.0/users/$mbx/mailfolders" #to get a list of folders
$appuri = "https://graph.microsoft.com/v1.0/users/$mbx/mailfolders/inbox" #using common folder names
$appuri = "https://graph.microsoft.com/v1.0/users/$mbx/mailfolders/AAMkADY3ODhkMDNlLTRjM2YtNDJjYi04YTkzLTFmNjFhYTcyMjA2NwAuAAAAAAAM5kRh6DylRZ4XsImGGVClAQA8oulgK3qgQ659gFhlXH_UAAAAAAEMAAA=" #Using Base64 naming for any folder in mailbox
#3. Build your $RestSplat
$RestSplat = @{
URI = $appuri
Headers = $(get-accesstoken)
Method = 'GET'
ContentType = "application/json"
}
#4. Run the RestMethod to pull back the Folder(s)
$Folders = Invoke-RestMethod @RestSplat
#5. Display the Results
$Folders.Value
Note: GraphAPI only pulls back 10 items at a time. If you need to pull back more than 10 items, run the following Do-While Loop:
#To find all Folders in a mailbox, run this:
#Create your Collection Variable
$Folders = @()
#Build your Starting URI
$appuri = "https://graph.microsoft.com/v1.0/users/$mbx/mailfolders"
#Run a Do-While Loop
Do{
"Running $appUri"
$RestSplat = @{
URI = $appuri
Headers = $(get-accesstoken)
Method = 'GET'
ContentType = "application/json"
}
$Tempresults = Invoke-RestMethod @RestSplat
$Folders += $tempresults.value
$appuri = $tempresults."@odata.NextLink"
}While($appuri -ne $null)
#Display your Messages
$Folders