Silently connect to 2FA enabled Exchange Server by remote Powershell

Mark Babayev 226 Reputation points
2020-07-20T18:46:31.7+00:00

I can silently (without using UI and popups) connect to a Exchange Server by remote Powershell with a Basic authentication:

$Password = ConvertTo-SecureString -AsPlainText "xxxxx" -Force                                                                                            
$Creds = New-Object System.Management.Automation.PSCredential -ArgumentList "xxxxxxx@xxx.com", $Password  
Connect-ExchangeOnline -Credential $Creds  

But here I receive an error because there is enabled 2FA. If I just execute "Connect-ExchangeOnline", it will show popup that I cannot afford because this script should run at the server side. I also tried to connect using JWT access_tokens from device-login authentication:
https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-oapx/71c8293a-e5d1-4498-a9da-873a7dc8c946

but it also doesn't work:

$Password = ConvertTo-SecureString -AsPlainText "Bearer DEVICE_TOKEN" -Force  
$Creds = New-Object System.Management.Automation.PSCredential -ArgumentList "xxxxxxx@xxx.com", $Password  
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/PowerShell-LiveId?BasicAuthToOAuthConversion=true -Credential $Creds -Authentication Basic -AllowRedirection  
Exchange Server Management
Exchange Server Management
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Management: The act or process of organizing, handling, directing or controlling something.
7,345 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
19,455 questions
{count} votes

Accepted answer
  1. Andy David 701 Reputation points
    2020-07-20T19:28:52.82+00:00
    1 person found this answer helpful.
    0 comments No comments

8 additional answers

Sort by: Most helpful
  1. KyleXu-MSFT 26,206 Reputation points
    2020-07-21T02:13:09.767+00:00

    Multi-factor authentication is a dynamic verification which will be used after verifying account and password. You cannot used it to silent connection to Exchange online PowerShell.

    So, if you still want to connect to Exchange online PowerShell silently, you may need to take Andy's suggestion to use Cert-based AUTH replace account and password. Or create a dedicated admin account which doesn't enabled MFA.

    1 person found this answer helpful.
    0 comments No comments

  2. Andy David - MVP 141.5K Reputation points MVP
    2020-07-23T13:55:03.253+00:00

    Ok, please see my previous response.
    Dont use New-ExoPSSession

    Per that doc:

    Examples:

    Connect-ExchangeOnline -CertificateFilePath "C:\Users\johndoe\Desktop\automation-cert.pfx" -AppID "36ee4c6c-0812-40a2-b820-b22ebd02bce3" -Organization "contosoelectronics.onmicrosoft.com"

    Connect-ExchangeOnline -CertificateThumbPrint "012THISISADEMOTHUMBPRINT" -AppID "36ee4c6c-0812-40a2-b820-b22ebd02bce3" -Organization "contosoelectronics.onmicrosoft.com"

    Also make sure you have setup the app in Azure correctly. All those steps in the docs that I linked earlier need to be followed
    you will also need to supply the private key password of pfx as a secure string

    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.security/convertto-securestring?view=powershell-7

    1 person found this answer helpful.
    0 comments No comments

  3. Andy David - MVP 141.5K Reputation points MVP
    2020-07-27T12:01:21.077+00:00

    Hi @MarkBabayev-6068

    Yes, if you are running this against another tenant, then an admin who has access to consent will need to allow that app. There is nothing required on the "Exchange" side. This is an Azure app which will have the same permissions as an Exchange Administrator Role in Office 365 and can manage as the Exchange admin

    You assign the app in the tenant where its being used. You tenant doesnt have access to their Exchange objects.
    Make sense?

    1 person found this answer helpful.
    0 comments No comments

  4. Andy David - MVP 141.5K Reputation points MVP
    2020-07-23T11:00:03.713+00:00

    You arent using the correct steps.
    Per that doc:

    Examples:

    Connect-ExchangeOnline -CertificateFilePath "C:\Users\johndoe\Desktop\automation-cert.pfx" -AppID "36ee4c6c-0812-40a2-b820-b22ebd02bce3" -Organization "contosoelectronics.onmicrosoft.com"

    Connect-ExchangeOnline -CertificateThumbPrint "012THISISADEMOTHUMBPRINT" -AppID "36ee4c6c-0812-40a2-b820-b22ebd02bce3" -Organization "contosoelectronics.onmicrosoft.com"

    Also make sure you have setup the app in Azure correctly. All those steps in the docs that I linked earlier need to be followed

    0 comments No comments