SOLVED

PowerShell - ActiveX Control Error

Copper Contributor

Greetings,

We have a script that connects to Exchange Online. The first time we run the script, it kicks back this error: "ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment." However, if we run the script a 2nd time, it works fine. What could be causing this? And how do I fix the script so that we don't have to keep running it twice?

Thanks,

Cole Riese

18 Replies
Both in PS 5 and 7 or?
Hi Harm, I am running PS version 5. Thanks!

Could you run this in an admin command prompt?
powershell.exe -noprofile
update-module ExchangeOnlineManagement
connect-Exchangeonline

That has been done
And? Screen output? Still the same?
Yes, I still got the ActiveX error
Could you share the part of the script which gives you the error?
This is the exact error that I get:

ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment.
At C:\Program Files\WindowsPowerShell\Modules\ExchangeOnlineManagement\3.1.0\netFramework\ExchangeOnlineManagement.psm1:729 char:21
+ throw $_.Exception.InnerException;
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], ThreadStateException
+ FullyQualifiedErrorId : ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment.
Ok, but could you share the part of the script and what line presents you with the error?
I'm not sure which line is kicking out that error. We don't have 729 lines in the script.
The 729 line is in the ExchangeOnlineManagement module... If you open your script in ISE or VSCode and run a few lines at a time, you know what line or action causes the error to appear.

Can you reproduce this on every client in PowerShell 5? Does it work without errors in 7?
Yes, other users running PowerShell 5 are getting the error message. The script seems to run fine in 7, however, we want to run the script in ISE because there are visual prompts
If the script runs fine the second time, it could be a timing issue in initializing specific code. But again, without showing that part of the script... It's just guessing...
best response confirmed by ColeRiese (Copper Contributor)
Solution

@ColeRiese Does your script utilize any forms? (e.g. $form1 = New-Object System.Windows.Forms.Form)

 

I just got done going through this and was finally able to work around it. It is my understanding that there is a bug in newer versions of the Exchange Online module. If you are using any sort of forms in your script and those forms are called before you connect to Exchange Online it generates the error. If you connect to Exchange Online first, then the form is called, there is no issue.

 

I had to tweak our script to connect to Exchange Online first, then add a prefix to our on-prem exchange session to prevent any conflicts. Here are a couple links I found during my search that helped me track down the issue. 

 

https://twitter.com/DanielSjoegren/status/1406954671248396292

https://www.sapien.com/forums/viewtopic.php?t=15297

https://www.sapien.com/forums/viewtopic.php?t=16213&sid=b1fcd3f71b3ada945eeb75f4dbc4490f&start=10

 

Hope this helps!

 

Thank you Pete! That seemed to do it!

Great! Glad that worked!

@Pete_Martin I am also getting this error but I am not running a script, I simply did a connect-exchangeonline with my UPN and it gave me this error. I am in an admin powershell window that is new, I have only imported the module and updated get-help.

I had the same issue as the original post and this resolved my issue. Thank you!
1 best response

Accepted Solutions
best response confirmed by ColeRiese (Copper Contributor)
Solution

@ColeRiese Does your script utilize any forms? (e.g. $form1 = New-Object System.Windows.Forms.Form)

 

I just got done going through this and was finally able to work around it. It is my understanding that there is a bug in newer versions of the Exchange Online module. If you are using any sort of forms in your script and those forms are called before you connect to Exchange Online it generates the error. If you connect to Exchange Online first, then the form is called, there is no issue.

 

I had to tweak our script to connect to Exchange Online first, then add a prefix to our on-prem exchange session to prevent any conflicts. Here are a couple links I found during my search that helped me track down the issue. 

 

https://twitter.com/DanielSjoegren/status/1406954671248396292

https://www.sapien.com/forums/viewtopic.php?t=15297

https://www.sapien.com/forums/viewtopic.php?t=16213&sid=b1fcd3f71b3ada945eeb75f4dbc4490f&start=10

 

Hope this helps!

 

View solution in original post