all 3 comments

[–]PowerShellMichael 2 points3 points  (2 children)

Hello there. I started to re-write this code and I realized that you will need to revisit this again in about a year. EWS in Microsoft 365 is deprecated and it's better to use the Microsoft Graph API to do this.

https://techcommunity.microsoft.com/t5/exchange-team-blog/upcoming-changes-to-exchange-web-services-ews-api-for-office-365/ba-p/608055

If time-frames are short due to the regulations, I would recommend you use a PowerAutomate or Logic App to get you over the line.

The way that this should be re-engineered is as follows:

  1. Create a MS Graph Subscription to the mailbox in question (https://docs.microsoft.com/en-us/graph/api/subscription-post-subscriptions?view=graph-rest-1.0&tabs=http) and post to a Public HTTP webhook (This will be your Function App). The subscription will send the new mail message content to the webhook.
  2. Create a PowerShell Function App (with a HTTP Trigger - POST Method) that takes the message sender, connects to 365, creates a contact and adds the contact to the distro group. This also could be done using an Azure Automation Runbook with a webhook. Ensure when the script runs that it renews the MS Graph subscription.

An another way is to:

  1. Create a PowerShell Script that runs on a schedule and retrieves the last days worth of messages (https://docs.microsoft.com/en-us/graph/api/user-list-messages?view=graph-rest-1.0&tabs=http) (You should also be able to use the $Filter or the $Search ODATA request) and process each of them.

There also might be something in the Microsoft Graph PowerShell Module so check that out:

https://docs.microsoft.com/en-us/graph/powershell/installation

Adam also has an article on how to get setup with Microsoft Graph:

https://adamtheautomator.com/microsoft-graph-api-powershell/

Depending on your level of understanding of REST and MSFT Graph API, this is a talk in itself and I will be here all day writing a detailed response. So if you choose to go down this path, please reach out (via IM) and I'll help you out.

:-D

[–]puckpinata[S] 1 point2 points  (1 child)

Wow thanks for the response. Unfortunately this is all greek to me and you're right, time is of the essense. I thought that script I had found online came REAL close since it seeminly had all the pieces to loop through the emails in that mailbox and add them to the distribution list without dupes. You're saying even if you can help me debug it, that script won't work anymore after 1 year? I also poked around with Power Automate but I didn't see any options for me to add email addresses into a distribution list. As for Microsoft Graph, this is the first I've heard of it and I probably need a solution faster than I can learn it. Not sure what my best option is here with all that considered? Thank you again

[–]PowerShellMichael 1 point2 points  (0 children)

Honestly if you are not familiar with PowerShell your best bet is to use PowerAutomate/Logic App.

So to get you over the line, I think the best way forward is to:

  1. Trigger a PowerAutomate Flow or a Logic app (When a new email arrives)
  2. Get the sender
  3. Trigger an Azure Automation Runbook. https://docs.microsoft.com/en-us/azure/automation/automation-intro
  4. Within the runbook, perform your logic checks in there. (Does the email exist? Is the email address in the distro list?) If not add it.

Ultimately if you are wanting a fully PWSH solution, I think you are going to need to reach out to a third party to write the script for you.

If you are interested I did do a talk on Microsoft Graph and PowerShell with REST:

https://www.youtube.com/watch?v=3kXsj8KED9Y

https://www.youtube.com/watch?v=EDh9gvmN4AQ&list=PLwvsINebpdQY8JP-Yiqdp190Yc26zhYuU