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