Task Scheduler to connect to Exchange Online Powershell using MFA

%3CLINGO-SUB%20id%3D%22lingo-sub-172469%22%20slang%3D%22en-US%22%3ETask%20Scheduler%20to%20connect%20to%20Exchange%20Online%20Powershell%20using%20MFA%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-172469%22%20slang%3D%22en-US%22%3E%3CP%3EI%20need%20to%20schedule%20a%20task%20to%20run%20this%20script%20below.%20I%20am%20able%20to%20run%20the%20below%20script%20when%20I%20am%20logged%20on%20the%20server%2C%20however%20when%20I%20schedule%20this%20as%20a%20task%20to%20run%20as%20%22Run%20whether%20the%20user%20is%20logged%20on%20or%20not%22%2C%20it%20fails.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20script%3CBR%20%2F%3E***********************************************************************%3CBR%20%2F%3E%20%23To%20record%20the%20ps%20session%3C%2FP%3E%0A%3CP%3EStart-Transcript%20-path%20c%3A%5Ctemp%5CPSSession.txt%3CBR%20%2F%3E%20%3CBR%20%2F%3E%23%20Import%20the%20Exchange%20Online%20module%3C%2FP%3E%0A%3CP%3Ecd%20C%3A%5CUsers%5C%3CUSERNAME%3E%5CAppData%5CLocal%5CApps%5C2.0%5C%3CGUID%3E%5C%3CGUID%3E%5Cmicr..tion_c8rrr8aa06b0c4a9_0010.0000_48a5c30b19dd2125%3C%2FGUID%3E%3C%2FGUID%3E%3C%2FUSERNAME%3E%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3EImport-Module%20.%5CCreateExoPSSession.ps1%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23%20Write%20output%20after%20the%20exchange%20online%20module%20imported%3CBR%20%2F%3EWrite-Output%20%22Importing%20Module%20completed%22%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3E%23Connect%20to%20Exchange%20Online%3CBR%20%2F%3EConnect-EXOPSSession%20-UserPrincipalName%20user%40domain.com%3C%2FP%3E%0A%3CP%3E%23Write%20an%20output%20when%20connected%20to%20exchange%20online%3CBR%20%2F%3EWrite-Output%20%22Connecting%20to%20Exchange%20Online%20completed%22%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23Exp%3CBR%20%2F%3EGet-UMMailbox%20%3CUSERNAME%3E%20%7C%20Export-Csv%20c%3A%5Ctemp%5CUM_Mailbox.csv%3C%2FUSERNAME%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%22%3C%2FP%3E%0A%3CP%3E************************************************************************%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3EThe%20error%20I%20see%20in%20the%20transcript%20file%20is%3B%3C%2FP%3E%0A%3CP%3E-------------------------------------------------------------------------%3C%2FP%3E%0A%3CP%3EPS%26gt%3BTerminatingError(New-ExoPSSession)%3A%20%22Showing%20a%20modal%20dialog%20box%20or%20form%20when%20the%20application%20is%20not%20running%20in%20UserInteractive%20mode%20is%20not%20a%20valid%20operation.%20Specify%20the%20ServiceNotification%20or%20DefaultDesktopOnly%20style%20to%20display%20a%20notification%20from%20a%20service%20application.%22%26gt%3B%26gt%3B%20TerminatingError(New-ExoPSSession)%3A%20%22Showing%20a%20modal%20dialog%20box%20or%20form%20when%20the%20application%20is%20not%20running%20in%20UserInteractive%20mode%20is%20not%20a%20valid%20operation.%20Specify%20the%20ServiceNotification%20or%20DefaultDesktopOnly%20style%20to%20display%20a%20notification%20from%20a%20service%20application.%22%26gt%3B%26gt%3B%20TerminatingError(New-ExoPSSession)%3A%20%22Showing%20a%20modal%20dialog%20box%20or%20form%20when%20the%20application%20is%20not%20running%20in%20UserInteractive%20mode%20is%20not%20a%20valid%20operation.%20Specify%20the%20ServiceNotification%20or%20DefaultDesktopOnly%20style%20to%20display%20a%20notification%20from%20a%20service%20application.%22%3CBR%20%2F%3E%26gt%3B%26gt%3B%20TerminatingError(New-ExoPSSession)%3A%20%22Showing%20a%20modal%20dialog%20box%20or%20form%20when%20the%20application%20is%20not%20running%20in%20UserInteractive%20mode%20is%20not%20a%20valid%20operation.%20Specify%20the%20ServiceNotification%20or%20DefaultDesktopOnly%20style%20to%20display%20a%20notification%20from%20a%20service%20application.%22%3CBR%20%2F%3EShowing%20a%20modal%20dialog%20box%20or%20form%20when%20the%20application%20is%20not%20running%20in%20UserInteractive%20mode%20is%20not%20a%20valid%20operation.%20Specify%20the%20ServiceNotification%20or%20DefaultDesktopOnly%20style%20to%20display%20a%20notification%20from%20a%20service%20application.%3CBR%20%2F%3ENew-ExoPSSession%20%3A%20Showing%20a%20modal%20dialog%20box%20or%20form%20when%20the%20application%20is%20not%20running%20in%20UserInteractive%20mode%20is%3CBR%20%2F%3Enot%20a%20valid%20operation.%20Specify%20the%20ServiceNotification%20or%20DefaultDesktopOnly%20style%20to%20display%20a%20notification%20from%20a%3CBR%20%2F%3Eservice%20application.%3CBR%20%2F%3E--------------------------------------------------------------------------%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3EI%20have%20tried%20a%20workaround%20here%2C%20%3CBR%20%2F%3E1.%20Schedule%20the%20script%20to%20%22Run%20only%20when%20user%20is%20logged%20on%22%3CBR%20%2F%3E2.%20Disconnect%20or%20lock%20the%20server%20session%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EDoes%20anyone%20has%20any%20ideas%20how%20we%20can%20schedule%20the%20script%20without%20having%20to%20leave%20the%20server%20session%20open%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-172469%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExchange%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-389731%22%20slang%3D%22en-US%22%3ERe%3A%20Task%20Scheduler%20to%20connect%20to%20Exchange%20Online%20Powershell%20using%20MFA%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-389731%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F78995%22%20target%3D%22_blank%22%3E%40Damon%20Villar%3C%2FA%3E%26nbsp%3BYup%2C%20that's%20correct.%26nbsp%3B%20I%20don't%20think%20you'll%20find%20a%20way%20to%20supply%20the%20credentials%20in%20your%20script%20and%20then%20say%2C%20have%20your%20phone%20with%20you%20while%20you're%20out%20and%20about%20so%20you%20can%20approve%20it%20in%20the%20Authenticator%20app.%26nbsp%3B%20That%20would%20be%20kind%20of%20cool%20though.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-389701%22%20slang%3D%22en-US%22%3ERe%3A%20Task%20Scheduler%20to%20connect%20to%20Exchange%20Online%20Powershell%20using%20MFA%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-389701%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F64125%22%20target%3D%22_blank%22%3E%40Jeremy%20Bradshaw%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20got%20the%20same%20error%20message%20when%20I%20tried%20to%20connect%20with%20an%20MFA%20enabled%20account.%20Is%20the%20only%20solution%20to%20run%20(powershell)%20scheduled%20task%20using%20an%20account%20without%20MFA%20enabled.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-333799%22%20slang%3D%22en-US%22%3ERe%3A%20Task%20Scheduler%20to%20connect%20to%20Exchange%20Online%20Powershell%20using%20MFA%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-333799%22%20slang%3D%22en-US%22%3E%3CP%3E-Credential%20only%20works%20if%20you%20don't%20have%20MFA%20enabled.%20For%20those%20that%20do%2C%20this%20is%20what%20happens%3A%3CBR%20%2F%3E%26gt%3BConnect-EXOPSSession%20-credential%20(Get-Credential%20user%40tenant.onmicrosoft.com)%3CBR%20%2F%3ENew-ExoPSSession%20%3A%20AADSTS50076%3A%20Due%20to%20a%20configuration%20change%20made%20by%20your%20administrator%2C%20or%3CBR%20%2F%3Ebecause%20you%20moved%20to%20a%20new%20location%2C%20you%20must%20use%20multi-factor%20authentication%20to%20access%3CBR%20%2F%3E'00000002-0000-0ff1-ce00-000000000000'.%3CBR%20%2F%3ETrace%20ID%3A%200447ae7b-0eb7-458a-b479-c73b66af5700%3CBR%20%2F%3ECorrelation%20ID%3A%205a85f56a-8468-4ca3-9739-95496153e12a%3CBR%20%2F%3ETimestamp%3A%202019-02-10%2013%3A16%3A30Z%3CBR%20%2F%3EAt%20C%3A%5CUsers%5Cjerem%5CAppData%5CLocal%5CApps%5C2.0%5CBRCQW4A8.VAY%5CHBOMCBME.APA%5Cmicr..tion_1975b8453054a2b5_0010%3CBR%20%2F%3E.0000_10d85008035862c6%5CCreateExoPSSession.ps1%3A292%20char%3A30%3CBR%20%2F%3E%2B%20...%20PSSession%20%3D%20New-ExoPSSession%20-UserPrincipalName%20%24UserPrincipalName.Va%20...%3CBR%20%2F%3E%2B%20~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%3CBR%20%2F%3E%2B%20CategoryInfo%20%3A%20NotSpecified%3A%20(%3A)%20%5BNew-ExoPSSession%5D%2C%20AdalServiceException%3CBR%20%2F%3E%2B%20FullyQualifiedErrorId%20%3A%20Microsoft.IdentityModel.Clients.ActiveDirectory.AdalServiceException%3CBR%20%2F%3E%2CMicrosoft.Exchange.Management.ExoPowershellSnapin.NewExoPSSession%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-176789%22%20slang%3D%22en-US%22%3ERe%3A%20Task%20Scheduler%20to%20connect%20to%20Exchange%20Online%20Powershell%20using%20MFA%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-176789%22%20slang%3D%22en-US%22%3E%3CP%3EThat%20error%20indicates%20an%20issue%20on%20MS%20side%2C%20best%20open%20a%20support%20case.%20The%20module%20works%20just%20fine%20with%20-Credentials%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20979px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F31084i1E49D8C718D75DC8%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22ExOMFAcreds2.png%22%20title%3D%22ExOMFAcreds2.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-176758%22%20slang%3D%22en-US%22%3ERe%3A%20Task%20Scheduler%20to%20connect%20to%20Exchange%20Online%20Powershell%20using%20MFA%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-176758%22%20slang%3D%22en-US%22%3E%3CP%3EI%20went%20to%20Office%20365%20exchange%20admin%20center%20%3D%26gt%3B%20hybrid%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20exchange%20online%20powershell%20module%2C%20clicked%20on%20configure%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Ethen%20opened%26nbsp%3B%20the%20shortcut%20to%20Microsoft%20Exchange%20Online%20Powershell%20Module%20from%20desktop%3C%2FP%3E%0A%3CP%3Etyped%20the%20following%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%24cred%20%3D%20Get-credential%26nbsp%3B%20(%20Entered%20credential%20in%20the%20format%20user%40domain.com)%3C%2FP%3E%0A%3CP%3EConnect-EXOPSSession%20-Credential%20%24cred%3C%2FP%3E%0A%3CP%3ENew-ExoPSSession%20%3A%20wstrust_endpoint_not_found%3A%20WS-Trust%20endpoint%20not%20found%20in%20metadata%20document%3CBR%20%2F%3EAt%20C%3A%5CUsers%5C%3CUSERNAME%3E%5CAppData%5CLocal%5CApps%5C2.0%5CXOB1V3KL.06M%5C82LDHNN9.8KN%5Cmicr..tion_a8dgft24shidhnb0c4a7_0010.0000_46a3c55dd9dd5128%5CCreateExoP%3CBR%20%2F%3ESSession.ps1%3A187%20char%3A22%3CBR%20%2F%3E%2B%20...%20PSSession%20%3D%20New-ExoPSSession%20-UserPrincipalName%20%24UserPrincipalName%20-C%20...%3CBR%20%2F%3E%2B%20~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%3CBR%20%2F%3E%20%2B%20CategoryInfo%20%3A%20NotSpecified%3A%20(%3A)%20%5BNew-ExoPSSession%5D%2C%20AdalException%3CBR%20%2F%3E%20%2B%20FullyQualifiedErrorId%20%3A%20Microsoft.IdentityModel.Clients.ActiveDirectory.AdalException%2CMicrosoft.Exchange.Management.ExoPowershe%3CBR%20%2F%3E%20llSnapin.NewExoPSSession%3C%2FUSERNAME%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-173925%22%20slang%3D%22en-US%22%3ERe%3A%20Task%20Scheduler%20to%20connect%20to%20Exchange%20Online%20Powershell%20using%20MFA%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-173925%22%20slang%3D%22en-US%22%3E%3CP%3EIt's%20not%20mandatory%20anymore%2C%20make%20sure%20you%20update%20your%20ExO%20module.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-173890%22%20slang%3D%22en-US%22%3ERe%3A%20Task%20Scheduler%20to%20connect%20to%20Exchange%20Online%20Powershell%20using%20MFA%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-173890%22%20slang%3D%22en-US%22%3E%3CP%3EThanks%20Vasil.%20However%20as%20I%20understand%2C%20-Userprincipalname%20is%20a%20mandatory%20parameter%2C%20i%20cannot%20skip%20it.%20I%20have%20tried%20using%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23%20Enter%20credential%20in%20format%20user%40domain.com%3C%2FP%3E%0A%3CP%3E%24cred%20%3D%20Get-Credential%3C%2FP%3E%0A%3CP%3EConnect-EXOPSSession%20-Credential%20%24cred%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EBut%20this%20fails%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENew-ExoPSSession%20%3A%20user_realm_discovery_failed%3A%20User%20realm%20discovery%20failed%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-172476%22%20slang%3D%22en-US%22%3ERe%3A%20Task%20Scheduler%20to%20connect%20to%20Exchange%20Online%20Powershell%20using%20MFA%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-172476%22%20slang%3D%22en-US%22%3E%3CP%3EUsing%20the%20-UserPrincipalName%20parameter%20will%20force%20the%20ADAL%20dialog%20to%20show%2C%20which%20is%20only%20possible%20in%20interactive%20sessions%20as%20the%20error%20message%20suggests.%20Even%20if%20you%20configure%20the%20session%20as%20interactive%2C%20someone%20will%20have%20to%20actually%20enter%20the%20credentials%20before%20the%20script%20completes.%20Instead%2C%20I%20would%20suggest%20you%20use%20the%20-Credential%20parameter%20and%20combine%20it%20with%20one%20of%20the%20methods%20to%20securely%20store%20credentials%20for%20reuse%20with%20PowerShell%2C%20for%20example%20this%3A%20%3CA%20href%3D%22https%3A%2F%2Fpractical365.com%2Fblog%2Fsaving-credentials-for-office-365-powershell-scripts-and-scheduled-tasks%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fpractical365.com%2Fblog%2Fsaving-credentials-for-office-365-powershell-scripts-and-scheduled-tasks%2F%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-870426%22%20slang%3D%22en-US%22%3ERe%3A%20Task%20Scheduler%20to%20connect%20to%20Exchange%20Online%20Powershell%20using%20MFA%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-870426%22%20slang%3D%22en-US%22%3E%3CP%3ELooks%20I'm%20running%20into%20the%20same%20issue.When%20I%20execute%20the%20powershell%20scripts%20which%20includes%26nbsp%3BConnect-EXOPSSession%20works%20fine%20but%20when%20same%20script%20scheduled%20through%20Task%20Scheduler%20is%20not%20able%20to%20proceed%20further%20after%26nbsp%3BConnect-EXOPSSession.It%20simply%20says%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ETask%20Scheduler%20successfully%20completed%20task%20%22%22%20%2C%20instance%20%22%7B3%7D%22%20%2C%20action%20%22C%3A%5CWindows%5CSystem32%5CWindowsPowerShell%5Cv1.0%5Cpowershell.exe%22%20with%20return%20code%202147942401.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1504432%22%20slang%3D%22en-US%22%3ERe%3A%20Task%20Scheduler%20to%20connect%20to%20Exchange%20Online%20Powershell%20using%20MFA%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1504432%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F78995%22%20target%3D%22_blank%22%3E%40Damon%20Villar%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EMicrosoft%20has%20released%20EXO%20V2%20preview%20module%20for%20non-interactive%20PowerShell%20scripts%20using%20Modern%20authentication%2FMFA.%3CBR%20%2F%3E%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fo365reports.com%2F2020%2F07%2F04%2Fmodern-auth-and-unattended-scripts-in-exchange-online-powershell-v2%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20ugc%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fo365reports.com%2F2020%2F07%2F04%2Fmodern-auth-and-unattended-scripts-in-exchange-online-powershell-v2%2F%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
New Contributor

I need to schedule a task to run this script below. I am able to run the below script when I am logged on the server, however when I schedule this as a task to run as "Run whether the user is logged on or not", it fails.

 

The script
***********************************************************************
#To record the ps session

Start-Transcript -path c:\temp\PSSession.txt

# Import the Exchange Online module

cd C:\Users\<username>\AppData\Local\Apps\2.0\<GUID>\<GUID>\micr..tion_c8rrr8aa06b0c4a9_0010.0000_48a5c30b19dd2125


Import-Module .\CreateExoPSSession.ps1

 

# Write output after the exchange online module imported
Write-Output "Importing Module completed"

 


#Connect to Exchange Online
Connect-EXOPSSession -UserPrincipalName user@domain.com

#Write an output when connected to exchange online
Write-Output "Connecting to Exchange Online completed"

 

#Exp
Get-UMMailbox <username> | Export-Csv c:\temp\UM_Mailbox.csv

 

"

************************************************************************


The error I see in the transcript file is;

-------------------------------------------------------------------------

PS>TerminatingError(New-ExoPSSession): "Showing a modal dialog box or form when the application is not running in UserInteractive mode is not a valid operation. Specify the ServiceNotification or DefaultDesktopOnly style to display a notification from a service application."
>> TerminatingError(New-ExoPSSession): "Showing a modal dialog box or form when the application is not running in UserInteractive mode is not a valid operation. Specify the ServiceNotification or DefaultDesktopOnly style to display a notification from a service application."
>> TerminatingError(New-ExoPSSession): "Showing a modal dialog box or form when the application is not running in UserInteractive mode is not a valid operation. Specify the ServiceNotification or DefaultDesktopOnly style to display a notification from a service application."
>> TerminatingError(New-ExoPSSession): "Showing a modal dialog box or form when the application is not running in UserInteractive mode is not a valid operation. Specify the ServiceNotification or DefaultDesktopOnly style to display a notification from a service application."
Showing a modal dialog box or form when the application is not running in UserInteractive mode is not a valid operation. Specify the ServiceNotification or DefaultDesktopOnly style to display a notification from a service application.
New-ExoPSSession : Showing a modal dialog box or form when the application is not running in UserInteractive mode is
not a valid operation. Specify the ServiceNotification or DefaultDesktopOnly style to display a notification from a
service application.
--------------------------------------------------------------------------


I have tried a workaround here,
1. Schedule the script to "Run only when user is logged on"
2. Disconnect or lock the server session

 

Does anyone has any ideas how we can schedule the script without having to leave the server session open?

10 Replies
Highlighted

Using the -UserPrincipalName parameter will force the ADAL dialog to show, which is only possible in interactive sessions as the error message suggests. Even if you configure the session as interactive, someone will have to actually enter the credentials before the script completes. Instead, I would suggest you use the -Credential parameter and combine it with one of the methods to securely store credentials for reuse with PowerShell, for example this: https://practical365.com/blog/saving-credentials-for-office-365-powershell-scripts-and-scheduled-tas...

Highlighted

Thanks Vasil. However as I understand, -Userprincipalname is a mandatory parameter, i cannot skip it. I have tried using

 

# Enter credential in format user@domain.com

$cred = Get-Credential

Connect-EXOPSSession -Credential $cred

 

But this fails 

New-ExoPSSession : user_realm_discovery_failed: User realm discovery failed

 

Highlighted

It's not mandatory anymore, make sure you update your ExO module.

Highlighted

I went to Office 365 exchange admin center => hybrid 

For exchange online powershell module, clicked on configure

 

then opened  the shortcut to Microsoft Exchange Online Powershell Module from desktop

typed the following;

 

$cred = Get-credential  ( Entered credential in the format user@domain.com)

Connect-EXOPSSession -Credential $cred

New-ExoPSSession : wstrust_endpoint_not_found: WS-Trust endpoint not found in metadata document
At C:\Users\<username>\AppData\Local\Apps\2.0\XOB1V3KL.06M\82LDHNN9.8KN\micr..tion_a8dgft24shidhnb0c4a7_0010.0000_46a3c55dd9dd5128\CreateExoP
SSession.ps1:187 char:22
+ ... PSSession = New-ExoPSSession -UserPrincipalName $UserPrincipalName -C ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [New-ExoPSSession], AdalException
+ FullyQualifiedErrorId : Microsoft.IdentityModel.Clients.ActiveDirectory.AdalException,Microsoft.Exchange.Management.ExoPowershe
llSnapin.NewExoPSSession

 

Highlighted

That error indicates an issue on MS side, best open a support case. The module works just fine with -Credentials:

 

ExOMFAcreds2.png

Highlighted

-Credential only works if you don't have MFA enabled. For those that do, this is what happens:
>Connect-EXOPSSession -credential (Get-Credential user@tenant.onmicrosoft.com)
New-ExoPSSession : AADSTS50076: Due to a configuration change made by your administrator, or
because you moved to a new location, you must use multi-factor authentication to access
'00000002-0000-0ff1-ce00-000000000000'.
Trace ID: 0447ae7b-0eb7-458a-b479-c73b66af5700
Correlation ID: 5a85f56a-8468-4ca3-9739-95496153e12a
Timestamp: 2019-02-10 13:16:30Z
At C:\Users\jerem\AppData\Local\Apps\2.0\BRCQW4A8.VAY\HBOMCBME.APA\micr..tion_1975b8453054a2b5_0010
.0000_10d85008035862c6\CreateExoPSSession.ps1:292 char:30
+ ... PSSession = New-ExoPSSession -UserPrincipalName $UserPrincipalName.Va ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [New-ExoPSSession], AdalServiceException
+ FullyQualifiedErrorId : Microsoft.IdentityModel.Clients.ActiveDirectory.AdalServiceException
,Microsoft.Exchange.Management.ExoPowershellSnapin.NewExoPSSession

Highlighted

@Jeremy Bradshaw 

I got the same error message when I tried to connect with an MFA enabled account. Is the only solution to run (powershell) scheduled task using an account without MFA enabled.

 

Highlighted

@Damon Villar Yup, that's correct.  I don't think you'll find a way to supply the credentials in your script and then say, have your phone with you while you're out and about so you can approve it in the Authenticator app.  That would be kind of cool though.

Highlighted

Looks I'm running into the same issue.When I execute the powershell scripts which includes Connect-EXOPSSession works fine but when same script scheduled through Task Scheduler is not able to proceed further after Connect-EXOPSSession.It simply says 

 

Task Scheduler successfully completed task "" , instance "{3}" , action "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" with return code 2147942401.

Highlighted

@Damon Villar 

 

Microsoft has released EXO V2 preview module for non-interactive PowerShell scripts using Modern authentication/MFA.
https://o365reports.com/2020/07/04/modern-auth-and-unattended-scripts-in-exchange-online-powershell-...