Read from Outlook using PowerShell Script and MAPI
I was asked how to read mails from outlook (Not Exchange), using PowerShell Script and execute another Script based on some criteria’s.
This challenge I found a little more difficult than a first assumed, but I put together this script, that you can download and use.
- Windows 2012 R2 64 BIT
- Outlook 2013 Connected to a “@live.com” mailbox
- Alert mails arrive in mailbox from System Center and other monitor tools, with certain error codes and flags
- Schedule task is looking for this in given interval
- When specific mails arrive various executions should take place
- Programmed in PowerShell
Attached you will find a ZIP file with a PowerShell script that will do exactly this. I have tried to explain in the script comment blocks what happens.
There are some prerequisites for this script to be able to run that you can read below.
Beside the PS1 file, you will also find a TXT file that shows all the properties on the Mail Item you can retrieve and use in your code as criteria’s in your IF, SWITCH or other code element to execute various actions.
Do not forget that this does not only apply to mail items, but could also be executed on Calendar items, Task or any other Item presented through outlook. This include Public folders if your outlook is connected to an Exchange Server. Below find the list of identifiers for a list of elements present when connecting to Exchange. Please remember that this could be different if your MAPI client is connected to Live, G-Mail, Lotus Notes, or any other mail server
- $olAppointmentItem = 1
- $olFolderDeletedItems = 3
- $olFolderOutbox = 4
- $olFolderSentMail = 5
- $olFolderInbox = 6
- $olFolderCalendar = 9
- $olFolderContacts = 10
- $olFolderJournal = 11
- $olFolderNotes = 12
- $olFolderTasks = 13
- $olFolderDrafts = 16
- $olPublicFoldersAllPublicFolders = 18
- $olFolderConflicts = 19
- $olFolderSyncIssues = 20
- $olFolderLocalFailures = 21
- $olFolderServerFailures = 22
- $olFolderJunk = 23;
The basic flow in the example is however that the scripts logs on to the current users outlook mailbox -> Iterates through all unread mails -> mark them as unread -> executed some CODE.
All mails that is already read, will be archived in a dedicated archive folder.
I hope this will give you an idea of how this works and help you refine your own script the best possible way. Please report if you find some cool techniques that could improve this
Before you begin, you must assign a server with Windows 2012 R2 installed that should execute the script. You must also have a service account that has access to the mailbox where your alert mails arrives. (Let us call him SVC-MAIL)
You must now sign into the server with the SVC-MAIL account. This is needed because we need a user profile for this user on the machine, to configure the outlook client
When logged in as this user you must setup outlook (2013 in my case), to connect and receive the mails in question. There are some settings you should setup to ensure that mails arrive as fast as possible, and to keep your mailbox clean.
Send / Recieve
Schedule every minute and Click all values
Empty Deleted folder
After this you must installed the Redemption tool
Outlook Redemption – The power of Extended MAPI in any language.
This tools is needed to avoid the security alert, please make sure that you do not violate the Usage terms and Conditions when using this tool
You install the tool both in 32 & 64 Bit version, as part of the basic install
You must create a folder in Outlook to Archive you mails in. I created this under my ROOT and called it “Archive” as shown in the illustration
Change the parameter in the script
You must open the script and change the parameters, in the beginning of the script before you execute
Last you need to setup the script as a scheduled task
So you just open scheduled task -> create a new task where you run the Action “Powershell” with the following argument
–Noninteractive -command “& ‘<Path>\ReadOutLook.ps1’ “
I hope you find this script usefull, please give som feedback on the script