Hi, @Roman King
It sounds like your problem is in a script, please understand that the Exchange Online tag is not focused on script issues at this time. Since you have added the Power Shell tag, you can wait for more professional guidance from them.
From my personal point of view, you can try the following suggestions:
- Use a for loop to iterate over the collection. This prevents the enumerator from throwing an exception when the collection is changed.
- Create a separate collection to store the threads to be aborted.
- Load 'Import-Module ExchangeOnlineManagement' at the beginning of the script
- Change parallel processing to serial processing, it may avoid the problem of concurrent modifications.
- Move the permission acquisition functions outside of the main script body to ensure they are available across the scope of the script.
If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".