MS O365 Exchange email input to PowerShell output

Question

Can we monitor a Microsoft Exchange server in the cloud using Office365 and process the emails with a PowerShell output script?

General

This article will demonstrate how you can process unread emails from an MS O365 Exchange server and output the email as a text file using PowerShell.

File Processor:
Input: Email
Output: Script

Configuration

Let us assume that you have basic knowledge of the File Processor, we will only configure Input and Output.

Input

  1. Go to the Input and select Input Type: Email.
  2. In the Connection tab select for Type: MS Echange.
  3. Fill in the Login information
    Username: my@company.com
    Password: yoursecret
    Domain: company.com

    Exchange Web Services (EWS) URL: https://outlook.office365.com/EWS/Exchange.asmx
  4. In the Options tab, select the Folder, e.g. Inbox by clicking the Browse button. This is the email folder with the emails that you want to process.
    Data to return: Only the e-mail.

Output

  1. Go to the Output and select Output Type: Script.
  2. Click the Edit-button, behind the Script label to open the Script Editor.
  3. Set the Script language to PowerShell.
  4. For the script use the code below: It will take the email and write the contents to a unique file.
    Change the path K:\Winking\FileProcessor\out to your desired location.
function ProcessData($lg, $data, $parameters) {
    # add PowerShell code here 
    
		$data | Out-File -FilePath "K:\Winking\FileProcessor\out\data-$(Get-Date -Format FileDateTime).txt"

    $boolResult = $true; # boolean with result to return
    return $boolResult;
}

Post-Action

  1. At the Success-Post-Actions tab, select Type: Method and choose Method: Mark as read.

When finishing processing an unread email it will mark the email as read. File Processor only processes unread emails.

Result

At your configured folder K:\Winking\FileProcessor\out\ you should see several files e.g. data-20220303T1015169251.txt with content:

DataContents     :
DateTimeCreated  :
DateTimeReceived :
DataID           :
Worker           :
From             :
To               :
Bcc              :
CC               :
DateTimeSent     :
Subject          :
TextBody         :
HtmlBody         :
ConfigData       :

See also