by

Utilizing the Workplace365/Change 2016 REST API to entry Mailbox knowledge utilizing PowerShell half 5 Sending

Utilizing the Workplace365/Change 2016 REST API to entry Mailbox knowledge utilizing PowerShell half 5 Sending Following on from my earlier posts on this sequence, I’ve made quite a few modifications to the Exch-REST PowerShell  module to extend its performance.

The primary is that it now helps utilizing both the Microsoft Graph endpoint or the Outlook REST endpoint which has been default up to now. If you wish to know which endpoint you must use take a look at https://dev.workplace.com/chooseapiendpoint . The benefit of using the graph endpoint is that it means that you can hook into all the opposite Workplace365 providers like Teams,OneDrive,SharePoint, Onenote and so forth utilizing the identical endpoint and token (I will have some instance in future posts).

To make the most of the graph endpoint is straightforward all you must do is add one extra parameter if you create the token to specify you need the AccessToken for the graph eg

$Token = Get-AccessToken -MailboxName mailbox@area.com `
-ClientId 5471030d-f311-4c5d-91ef-74ca885463a7 `
-redirectUrl urn:ietf:wg:oauth:2.0:oob
-ResourceURL graph.microsoft.com

All the opposite cmdlets will then have a look at the AccessToken`s useful resource property utilizing the brand new Get-Endpoint perform and return the suitable endpoint. If you wish to use the preview endpoints you would want to tweak this perform to return the suitable preview endpoint.

perform Get-EndPoint{
        param(
        [Parameter(Position=0, Mandatory=$true)] [psObject]$AccessToken,
        [Parameter(Position=1, Mandatory=$true)] [psObject]$Section
    )
    Start{
        $EndPoint = "https://outlook.workplace.com/api/v2.0"
        swap($AccessToken.useful resource){
            "https://outlook.workplace.com" {  $EndPoint = "https://outlook.workplace.com/api/v2.0/" + $Section }    
            "https://graph.microsoft.com" {  $EndPoint = "https://graph.microsoft.com/v1.0/" + $Section  }    
        }
        return , $EndPoint
       
    }
}

The one quirk I’ve come throughout with the distinction between the 2 endpoints is when utilizing prolonged properties the Title used to determine the Property is PropertyId within the REST endpoint and Id in Graph Endpoint (when making GET). The module has partial protection for this however its an space that wants some work and can enhance with future updates.

I’ve additionally added another AccessToken creation perform that means that you can cross within the username and password by way of a PSCredential. This requires that you’ve got implicit authentication enabled in your app configuration manifest see oauth2AllowImplicitFlow

I’ve additionally added capabilities for Merchandise Creation, Sending messages and Attachment enumeration and downloading and it now shops the Tokens in securestrings.

Additionally because of Elijah Gagne there’s additionally some correct getting began documentation within the GitHub Repo  https://github.com/gscales/Exch-Relaxation/blob/grasp/README.md

I’ve added some samples for utilizing the Ship-MessageREST perform which is what you’ll use if you wish to ship a Message. This perform covers all of the Ship Message bases it could possibly ship a Message usually as a consumer, Ship a Message On Behalf or SendAS, and in addition it could possibly deal with permitting you to set the ReplyTo header to permit a unique reply to handle in addition to set Supply and Learn Recipients. The samples web page could be discovered https://github.com/gscales/Exch-Relaxation/blob/grasp/Samples/SendMessageExamples.md .

A full instance that will get an Entry token to ship a Message with an Attachment would appear to be

$Mailbox = "consumer@area.com"
$Topic = "Test Message"
$Physique = "Test Body of Message"
$To = @(New-EmailAddress -Deal with consumer@area.com)


$Attachment = @("c:tempattachmentfile.doc")



$AccessToken = Get-AccessToken -MailboxName $Mailbox -ClientId 5471030d-f311-4c5d-91ef-74ca885463a7 -redirectUrl urn:ietf:wg:oauth:2.0:oob
Ship-MessageREST -MailboxName $Mailbox  -AccessToken $AccessToken -ToRecipients $To -Topic $Topic -Physique $Physique -Attachments $Attachment

The module is offered from the PowerShell Gallery https://www.powershellgallery.com/packages/Exch-Relaxation and the supply from Github
https://github.com/gscales/Exch-Relaxation