Generic Folder Picker for Mailboxes to be used in REST based mostly Powershell Workplace365 / MSExchange Scripts

Generic Folder Picker for Mailboxes to be used in REST based mostly Powershell Workplace365 / MSExchange Scripts This can be a continuance of my Workplace365/Alternate 2016 Relaxation collection, on this put up I will be exhibiting how among the new GUI parts within the Exch-Relaxation module will help enhance the pace and value of any scripts the place that you must browse Mailbox folders. What this modification to the Module does is means that you can enumerate all of the Mail Folders in a Mailbox utilizing the REST API after which current a easy folder tree that then means that you can flick thru the folder in a Mailbox you may be on the lookout for and whenever you double click on that folder it’s going to then return that as an object that may be piped into additional operations. eg the brand new cmdlet works like

Invoke-MailFolderPicker -MailboxName -AccessToken $AccessToken

and visually that is what it seems like

I’ve one further characteristic I’ve added into the module that means that you can return the identical folder tree as above with the folder sizes in MB. To do that that you must make sure that of the Prolonged Property (PR_MESSAGE_SIZE_EXTENDED Mapi property). To make use of this run the Invoke-MailFolderPicker cmdlet with the -ShowFolderSize swap eg

Invoke-MailFolderPicker -MailboxName -AccessToken $AccessToken -ShowFolderSize 
will return one thing like
The way it works 

The Invoke-MailFolderPicker cmdlet is an amalgam of different capabilities within the Exch-Relaxation module (that is truly the primary purpose I have been constructing this module within the first place whereas the person capabilities inside the module are comparatively straightforward to breed you want a big library of those small capabilities to construct issues which might be actually fascinating and helpful). 
So to construct a Tree we first want a root so getting the Root Folder of the Mailbox is completed utilizing
$rootFolder = Get-RootMailFolder -AccessToken $AccessToken -MailboxName $MailboxName
The necessary half right here is to know the Id of the foundation folder so whenever you have a look at the mother or father FolderId properties of any youngster folder you possibly can assemble a Tree of folders from that.
To cater for Foldersizes if the ShowFolderSize swap  is used that you must use an Prolonged property as a result of there isn’t any strongly typed property for FolderSize that’s returned by the REST API (this was the identical in EWS as effectively). To do that the next code snippet is used
    $PropList = @()
    $FolderSizeProp = Get-TaggedProperty -Id “0x0E08” -DataType Lengthy
    $PropList += $FolderSizeProp
    $Folders = Get-AllMailFolders -MailboxName $MailboxName -AccessToken $AccessToken -PropList $PropList
This builds out the proper enlargement string for these prolonged properties in addition to catering for the variations between the graph and Outlook endpoints. The Get-AllMailFolder cmdlets simply will get all of the youngster folders of the Mailbox and returns them as a set. Lastly the 
 Invoke-FolderPicker -MailboxName $MailboxName -Folders $Folders -rootFolder $rootFolder -pickerType mail -ShowFolderSize
Utilizing the Home windows type and the Treeview management to construct a visible illustration of the Mailbox folder tree and return it to initiator.
Sensible use

I’ve give you a sensible pattern of this situated that can when you double click on the goal folder will return the final 10 objects in a folder to the PowerShell window. This can be a easy four line script together with the code to get the Token
$MailboxName = ""
$AccessToken = Get-AccessToken -MailboxName $MailboxName -ClientId 5471030d-f311-4c5d-91ef-74ca885463a7 -redirectUrl urn:ietf:wg:oauth:2.0:oob -ResourceURL
$FolderSelected = Invoke-MailFolderPicker -MailboxName $MailboxName -AccessToken $AccessToken -ShowFolderSize
Get-FolderItems -AccessToken $AccessToken -MailboxName $MailboxName -Folder $FolderSelected -Prime 10 -TopOnly:$true -ReturnSize | Choose Topic,

All of the scripts on this weblog require model 2.1 of the Exch-Relaxation module from the PowerShell gallery