ExchangeOnline module hangs

Tool: Visual Studio 2019 PS Pro Tools
Version: 2019. Compiled as exe, dotnet 4.72

My Project Compiles and runs fine… It loads the AD module, then the Aure AD module, but hangs loading the Exchange online module. Running the code direct on PS works fine.
Connect-ExchangeOnline -userprincipalname $msAccountName
The code gets as far as


then just sits there. Normally, I would see an authentication dialogue box.
Note, I need to use this method as we have MFA in play. Hvaen been trying to troubleshoot this for a couple of days, and I’m stumped!

Can you share the PS proj you are trying to compile like this? A couple things come to mind are the thread apartment state and x64 vs x86 but having a reference on the settings you are using would be helpful.

Hi Adam, happy to share with you directly, but wouldn’t want to put the code in the public arena (yet)
I can send you a onedrive link if that works?
Using PS x64

many thanks

Hi has there been any update on this? I’m running into this exact situation. I’m able to connect to exchange using the command “connect-exchangeonline” within powershell or powershell_ise but when it’s compiled into my application, it just hangs at that exact spot depicted in the screenshot of original post. Luckily, when I run a debug on my app, I’m able to dive into the actual command to see that it’s getting stuck on the following command (below) but I cannot understand why.

I’ve validated all powershell versions and exchangeonlinemanagement module is up to date. i even went down the rabbit hole to validate tls setting for .net is correct. i’m able to replicate this on two different Windows 10 machines at different OS versions (1809 & 20H2).

$PSSession = New-ExoPSSession -ExchangeEnvironmentName $ExchangeEnvironmentName -ConnectionUri $ConnectionUri -AzureADAuthorizationEndpointUri $AzureADAuthorizationEndpointUri -UserPrincipalName $UserPrincipalName.Value -PSSessionOption $PSSessionOption -Credential $Credential.Value -BypassMailboxAnchoring:$BypassMailboxAnchoring -DelegatedOrg $DelegatedOrganization

I found an article on sapien’s website that explains the issue very well. seems to be a combination of powershell version 5.1 and the exchangeonlinemanagement module being version 2.0.4 or later. i’m about to test this but so far it lines up with my two machines that don’t work but are newer and have later versions of the module. The alternative is to re-arrange code to make the connection before calling upon any new-object command or outside of an action code block. however, this does not work for me so looks like i’ll try rolling back exchange module versions until the (what i call a bug) is fixed.

I had to do the connect before loading the form, that way it worked

Jimmy White

Nice. yeah that was a workaround proposed but that wasn’t how i want my app to work.
I just confirmed the issue is what i previously posted. incompatibility between powershell 5.1 and exchangeonlinemanagement module 2.0.4 or later.

another fix is to either update to powershell 7.1 or downgrade the exchange module to 2.0.3. for now I just downgraded the exchange module and verified that worked

ISE was deprecated years ago and it is not being longer developed. I suggest you install VSCode which has a wonderful support for Powershell debugging and script editing.