EXOV3 and ActiveDirectory modules Non-Thread STA issue in PS5.1 and PSS 2023

This forum can be browsed by the general public. Posting is limited to current SAPIEN license holders with active maintenance and does not offer a response time guarantee.
Forum rules
DO NOT POST LICENSE NUMBERS, ACTIVATION KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 1 year and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
DarusDP
Posts: 43
Last visit: Fri Feb 09, 2024 1:02 pm
Been upvoted: 1 time

EXOV3 and ActiveDirectory modules Non-Thread STA issue in PS5.1 and PSS 2023

Post by DarusDP »

Product, version and build: Product: PowerShell Studio 2023 (64 Bit) - Build: v5.8.217
Operating system: Windows 10 Enterprise (64bits) 21H2 - Build: v10.0.19044.0
PowerShell version(s): 5.1

Using ExchangeOnlineManagement 3.1.0 (EXOV3) module in PowerShell Studio gets this error:
ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment.

This error occurs with STA Mode enabled and disabled.

This does not occur in PS ISE, nor with ExchangeOnlineManagement 2.0.3 (EXOV2)

I narrowed down the issue to import-module ActiveDirectory being in the script using EXOV3 and PS Studio as the only combination that gets an error.

The ActiveDirectory module and EXOV3 are essential to my script. I'm coding a GUI therefore I need this to work in PS Studio.

I'm at the latest version of PS Studio 2023, PS 5.1.x

Here is a chart of combinations
  1. PS Environment    EXOV    AD Module in script   Results
  2. PS ISE                      EXOV2      Yes                             Works
  3. PS ISE                      EXOV2      No                              Works
  4. PS ISE                      EXOV3      Yes                             Works
  5. PS ISE                      EXOV3      No                              Works
  6.  
  7. PSStudio                 EXOV2     Yes                             Works
  8. PSStudio                 EXOV2     No                              Works
  9. PSStudio                 EXOV3     Yes                             gets error list above
  10. PSStudio                 EXOV3     No                              Works
Microsoft is warning that EXOV2 will not works as of June 2023 and scripts need to use EXOV3 so I am working to that end. However, I ran into this issue.

Here is the code that does NOT work in PS Studio.
user@email.domain is in the code posted here but actual code has a real value that is valid.
Code execution fails on connect-exchangeonline and enters the catch block to display the error message.

Code: Select all

Import-Module ActiveDirectory
Import-module -Name ExchangeOnlineManagement -RequiredVersion 3.1.0
$error.Clear()
try
{
	Write-Host 'logging in with user account to EXO'
	$ErrorActionPreference = 'Stop'
	$ProgressPreference = 'SilentlyContinue'
	Connect-ExchangeOnline -UserPrincipalName "user@email.domain" -ShowBanner:$false
	$ErrorActionPreference = 'Continue'
	$a = Get-ConnectionInformation
	Write-Host "Connection ID: $($a.Id)"
	Disconnect-ExchangeOnline -Confirm:$false
}
Catch
{
	Write-Host $_
}
exit
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Re: EXOV3 and ActiveDirectory modules Non-Thread STA issue in PS5.1 and PSS 2023

Post by Alexander Riedel »

What is the version of the ActiveDirectory module you have?
Alexander Riedel
SAPIEN Technologies, Inc.
DarusDP
Posts: 43
Last visit: Fri Feb 09, 2024 1:02 pm
Been upvoted: 1 time

Re: EXOV3 and ActiveDirectory modules Non-Thread STA issue in PS5.1 and PSS 2023

Post by DarusDP »

The AD module is version 1.0.1.0
I looked into if there is a new version but the Powershell AD module comes with the OS and is part of the RSAT (Remote Systems Administration Tools), so I found no update.
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Re: EXOV3 and ActiveDirectory modules Non-Thread STA issue in PS5.1 and PSS 2023

Post by Alexander Riedel »

We cannot readily produce that here.
"Code execution fails on connect-exchangeonline and enters the catch block to display the error message."
Does that mean the login dialog comes up and fails AFTERWARDS or does the dialog not come up to begin with?
Alexander Riedel
SAPIEN Technologies, Inc.
DarusDP
Posts: 43
Last visit: Fri Feb 09, 2024 1:02 pm
Been upvoted: 1 time

Re: EXOV3 and ActiveDirectory modules Non-Thread STA issue in PS5.1 and PSS 2023

Post by DarusDP »

Login dialog does not come up, call to connect-exchangeonline fails in try block with the error msg in the post.
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Re: EXOV3 and ActiveDirectory modules Non-Thread STA issue in PS5.1 and PSS 2023

Post by Alexander Riedel »

Ok, here are a few things I would try. You may have done that already, but here it goes.
- Reboot. This is a silly one, but COM objects do stick around. Only to be sure is to reboot.
- If you use the ISE for testing, restart it every single time. It reuses the runspace so you do not get accurate results otherwise
- Since there is an interaction with the AD module from what you are seeing, move the import module statement for it until AFTER you do your login.

We are looking here what we can find out in the meantime.
Alexander Riedel
SAPIEN Technologies, Inc.
DarusDP
Posts: 43
Last visit: Fri Feb 09, 2024 1:02 pm
Been upvoted: 1 time

Re: EXOV3 and ActiveDirectory modules Non-Thread STA issue in PS5.1 and PSS 2023

Post by DarusDP »

ok, I will try those things.

I used the debugger and the line that produces the error is below.
This is Microsoft code from the ExchangeOnlineManagement v3.1.0 module
script = ExchangeOnlineManagement.psm1
line = 528

Not sure it will help much.

Code: Select all

$ConnectionContext = Get-ConnectionContext -ExchangeEnvironmentName $ExchangeEnvironmentName -ConnectionUri $ConnectionUri `
-AzureADAuthorizationEndpointUri $AzureADAuthorizationEndpointUri -UserPrincipalName $UserPrincipalName.Value `
-PSSessionOption $PSSessionOption -Credential $Credential.Value -BypassMailboxAnchoring:$BypassMailboxAnchoring `
-DelegatedOrg $DelegatedOrganization -Certificate $Certificate.Value -CertificateFilePath $CertificateFilePath.Value `
-CertificatePassword $CertificatePassword.Value -CertificateThumbprint $CertificateThumbprint.Value -AppId $AppId.Value `
-Organization $Organization.Value -Device:$Device.Value -InlineCredential:$InlineCredential.Value -CommandName $CommandName `
-FormatTypeName $FormatTypeName -Prefix $Prefix -PageSize $PageSize.Value -ExoModuleVersion:$moduleVersion -Logger $cmdletLogger `
-ConnectionId $connectionContextID -IsRpsSession $UseRPSSession.IsPresent -EnableErrorReporting:$EnableErrorReporting.Value `
-ManagedIdentity:$ManagedIdentity.Value -ManagedIdentityAccountId $ManagedIdentityAccountId.Value -AccessToken $AccessToken
Here are the values of all the parameters, many are null/blank. I have removed my email address since this post can be read by many. (See -UserPrincipalName user@email.com)

-ExchangeEnvironmentName O365Default
-ConnectionUri
-AzureADAuthorizationEndpointUri
-UserPrincipalName user@email.com
-PSSessionOption
-Credential
-BypassMailboxAnchoring:False
-DelegatedOrg
-Certificate
-CertificateFilePath
-CertificatePassword
-CertificateThumbprint
-AppId
-Organization
-Device:False
-InlineCredential:False
-CommandName *
-FormatTypeName *
-Prefix
-PageSize 1000
-ExoModuleVersion:3.1.0
-Logger Microsoft.Online.CSE.RestApiPowerShellModule.Instrumentation.CmdletLogger
-ConnectionId 1ebf5921-e7dd-45f0-9e79-ce12e29e7e2c
-IsRpsSession False
-EnableErrorReporting:False
-ManagedIdentity:False
-ManagedIdentityAccountId
-AccessToken
DarusDP
Posts: 43
Last visit: Fri Feb 09, 2024 1:02 pm
Been upvoted: 1 time

Re: EXOV3 and ActiveDirectory modules Non-Thread STA issue in PS5.1 and PSS 2023

Post by DarusDP »

Moving the import-module ActiveDirectory till after connect-exchangeonline seems to work.

I will have to do extensive reorganizing of my code (it's a hefty GUI app doing lots of M365 admin things) to make this a work around.

If you all could still pursue that would be appreciated.
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Re: EXOV3 and ActiveDirectory modules Non-Thread STA issue in PS5.1 and PSS 2023

Post by Alexander Riedel »

I have checked around with the debugger in our interface and hosting code and when you select STA all threads that we create are single threaded apartment.
You may get some more information asking Microsoft to investigate why these two modules can have this interaction.
Alexander Riedel
SAPIEN Technologies, Inc.
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Re: EXOV3 and ActiveDirectory modules Non-Thread STA issue in PS5.1 and PSS 2023

Post by Alexander Riedel »

As you can see here, with the sample code you provided it seems to operate.
Of course I do not have an actual AD server running here.
2023-02-03_13-17-04.png
2023-02-03_13-17-04.png (315.58 KiB) Viewed 5689 times
Alexander Riedel
SAPIEN Technologies, Inc.
This topic is 1 year and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.