NEW Connect-ExchangeOnline doesn't bring up MFA Capable browser based logonbox when executed from PowerShell Studio GUI

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

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 2 years and 9 months 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.
Locked
User avatar
PALCO78
Posts: 3
Last visit: Thu Feb 01, 2024 3:35 am

NEW Connect-ExchangeOnline doesn't bring up MFA Capable browser based logonbox when executed from PowerShell Studio GUI

Post by PALCO78 »

Hello
I ask the question again because i still have this exact problem described in "Connect-ExchangeOnline doesn't bring up MFA Capable browser based logonbox when executed from PowerShell Studio GUI"

I have tried all what was described even with PWS V7 with no success
I am unable to lauch a powershellStudio project with forms if i call connect-exchangeonline
I just want to add an information :
if i call connect-msolservice all is working perfectly. (i use the same MFA user !)
The problem occurs only when calling connect-exchangeonline
I am really disappointed because i only use powershell studio for some basic GUI for Office 365 actions
Thank's in advance for yours answers
User avatar
PALCO78
Posts: 3
Last visit: Thu Feb 01, 2024 3:35 am

Re: NEW Connect-ExchangeOnline doesn't bring up MFA Capable browser based logonbox when executed from PowerShell Studio

Post by PALCO78 »

I have found a workaround :
Install PowerShell Exchange Online Module from Exchange Management interface (hybrid section)

Then use this instead of connect-exchangeonline :
$MFAExchangeModule = (Get-ChildItem -Path $($env:LOCALAPPDATA + "\Apps\2.0\") -Filter CreateExoPSSession.ps1 -Recurse).FullName | select-object -last 1
. "$MFAExchangeModule"
connect-exopssession

For information : connect-exchangeonline works like a charm if the project is a simple powershellstudio script project (instead of being a Form project)
if any has a better idea....
User avatar
njkaepp7
Posts: 5
Last visit: Thu Aug 05, 2021 10:54 am

Re: NEW Connect-ExchangeOnline doesn't bring up MFA Capable browser based logonbox when executed from PowerShell Studio

Post by njkaepp7 »

Here is a simple example of it working/breaking (at lease for me in my environment).

In WPS 5.1 this works:
Connect-ExchangeOnline
Add-Type -AssemblyName System.Windows.Forms
$FormMain = New-Object System.Windows.Forms.Form
$FormMain.ShowDialog()
$FormMain.Dispose()

Note that Connect-ExchangeOnline is called BEFORE the New-Object. This allows the MFA (GUI) prompt to show up.

If I switch it to this example below instead, it never shows MFA (GUI) prompt (it just hangs):
Add-Type -AssemblyName System.Windows.Forms
$FormMain = New-Object System.Windows.Forms.Form
Connect-ExchangeOnline
$FormMain.ShowDialog()
$FormMain.Dispose()

Note that Connect-ExchangeOnline is called AFTER the New-Object. This hangs the MFA (GUI) prompt.

With PS 7, the MFA GUI prompt is replaced with a web-browser based MFA login (at least for me it does). In this case, either of the above examples work. Its only with WPS 5.1 does the second example fail.

Additionally, this only happens with the newer version of the Exchange module, where they added support for PS7. So whatever they did to make it work with PS7, I am guessing is what is also causing the new problem with WPS 5.1

One final note, if you cancel ANY of the MFA login prompts, it also seems to cause problems. So if you are testing the above, and cancel the MFA login process, you may not see the form or it may hang. You need to SUCCESSFULLY login to MFA to get a working example, from the above code. In my production code, I handle this with TRY/CATCH etc., which I have excluded in the above examples.

Hope this helps.
NK
User avatar
PALCO78
Posts: 3
Last visit: Thu Feb 01, 2024 3:35 am

Re: NEW Connect-ExchangeOnline doesn't bring up MFA Capable browser based logonbox when executed from PowerShell Studio

Post by PALCO78 »

thank's a lot for your answer !!
I never went to the "source" script file !

i now understand better your comment about New-Object 'System.Windows.Forms.Form' and the order actions (connect-exchangeonline before the forms command)
When i have read your comments in the first post i didn't realize that, because i was only using the script editor given by sapien which does not show the exact content (to simplify)

i will try
Again thank's !
Phil
This topic is 2 years and 9 months 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.
Locked