Forum Discussion

Abdul Rehman Shaikh's avatar
Abdul Rehman Shaikh
Copper Contributor
Mar 16, 2018

Task Scheduler to connect to Exchange Online Powershell using MFA

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?

  • 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-tasks/

    • Abdul Rehman Shaikh's avatar
      Abdul Rehman Shaikh
      Copper Contributor

      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

       

      • VasilMichev's avatar
        VasilMichev
        MVP

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

Resources