I've developed a tool for managing Exchange that provides various functionalities like managing mailboxes, users, Active Directory groups, and distribution groups. Until recently, everything was working smoothly with PowerShell 5 and I was converting my .ps1 script to .exe. However, I'm now encountering an error that says: "ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment." This error had popped up during development before, but I managed to resolve it by keeping the exchange management module version below 3.6.0, which had worked for months. Now, I've confirmed that version 3.6.0 is installed, but I'm unsure if this is related to the Microsoft.Graph module or something else. Interestingly, when I comment out the line `Connect-ExchangeOnline -ShowBanner:$false`, the GUI starts without a hitch. I've read that I should remove any forms before connecting to Exchange and loading modules, but that hasn't resolved the issue either. My ultimate fallback would be getting everyone on my team to upgrade to PowerShell 7, but I'm hoping for a more definitive solution. Has anyone else faced this problem and found a solid fix?
4 Answers
I had a similar issue when connecting to Exchange Online. It seems to throw that thread error the first time you run it, but usually connecting again without any changes works just fine.
Your tool sounds really interesting! Can you share more about how it operates?
Thanks! It's a reporting tool for Exchange Management that has various tabs like User, Shared Mailbox, Distribution Group, and AD Groups. Enter a user ID, and it pulls their mailbox size, calendar access details, and more. You can even export the info to CSV!
Using a ps1 to exe utility can cause various issues, and I suspect this might be the root of your problem. In my experience, using WinForms while calling the Exchange Online module can trigger these errors. A solution could be to run the EXO script as a separate executable and handle data transfer between them.
Yeah, I think you're onto something. It works fine in PowerShell 7. Instead of battling with the .exe conversion, transitioning everyone on my team to PowerShell 7 seems like a cleaner choice.
Check out how to switch from MTA to STA in your PowerShell session. The Integrated Scripting Environment often gets tricky because it uses a different threading model than the console. If possible, consider moving away from ActiveX entirely, especially since it's being phased out. Using an assembly instead could avoid these apartment model issues.
I've tried switching to STA, but it didn’t help. I might just opt for having everyone use PowerShell 7 and stick with the .ps1 file instead of converting to .exe.
I noticed that too at first, but now it consistently pops up each time I launch my tool. Connecting to Exchange Online outside of the script, though, seems to work smoothly.