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
- Go to the Input and select Input Type:
Email
. - In the Connection tab select for Type:
MS Echange
. - 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
- 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
- Go to the Output and select Output Type:
Script
. - Click the Edit-button, behind the Script label to open the Script Editor.
- Set the Script language to
PowerShell
. - For the script use the code below: It will take the email and write the contents to a unique file.
Change the pathK:\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
- 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 :