We have an Office365 connection configured to POST to Win-PS and execute PowerShell to change mailbox properties. The powershell payload sent over to Win-PS downloads a PS1 file from our own hosted URL and executes it with our specified parameters. We download it and execute this way to avoid complicated syntax escaping in the POST body.
The script PS1 essentially does a connect-exchangeonline (Microsoft ExchangeOnline V3) & connect-AzureAD and then runs a bunch of cmdlets. All in all, a 2-3 second run per updateAccount.
When I send less than 10 updateAccount tasks over, things work great.
When I send over many tasks (more than 10) things go wonky. Eventually, after a bunch of tasks running correctly, it loses the Exchange connection and cannot self recover. Even though the script is designed to test for a connection and do a new connect-exchangeonline, it doesn't work anymore after the initial breakdown. I even have the script locking things down to 1 connection at a time via a lock file so I know there are no conflicting sessions.
I'm thinking there's something shared or conflicting in the way the Win-PS C# handles the runspaces with PowerShell? I have very substantial logging out to our Splunk environment but have run out of ideas to try inside PowerShell to handle the breakdown correctly. Any thoughts? I've tried both as an import-module and a direct PS1 execute on the Win-PS side.. neither one had rock solid results.
Am I doing something unreasonable? Is there a better approach?