Forum Discussion
Task Scheduler to connect to Exchange Online Powershell using MFA
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/
- ashaikhMar 21, 2018Copper 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
- VasilMichevMar 21, 2018MVP
It's not mandatory anymore, make sure you update your ExO module.
- ashaikhMar 28, 2018Copper Contributor
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