

Connecting to Exchange Online Using Multi-Factor Authentication via PowerShell
source link: https://thomasrayner.ca/connecting-to-exchange-online-using-multi-factor-authentication-via-powershell/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Connecting to Exchange Online Using Multi-Factor Authentication via PowerShell
Using PowerShell to manage your Microsoft cloud services like Exchange Online is awesome. Using multi-factor authentication (MFA) is also awesome. For some reason, using the two together is not awesome. Many of the Microsoft docs on this seem to suggest you just perform all your administrative tasks from a shell that you launch entirely separately from a normal PowerShell console. I would rather be able to connect to Exchange Online using MFA via PowerShell through a normal console, or as part of another tool. Let me show you how.
The first thing you’ll need to do is install the tool at this page. This will give you all the tools and libraries you need to install to connect to Exchange Online using MFA via PowerShell, including that special, magic console. Now that you have the tools installed, you can use this snippet to connect from a normal PowerShell console or from within another PowerShell-based tool.
$modules = @(Get-ChildItem -Path "$($env:LOCALAPPDATA)\Apps\2.0" -Filter "Microsoft.Exchange.Management.ExoPowershellModule.manifest" -Recurse )
$moduleName = Join-Path $modules[0].Directory.FullName "Microsoft.Exchange.Management.ExoPowershellModule.dll"
Import-Module -FullyQualifiedName $moduleName -Force
$scriptName = Join-Path $modules[0].Directory.FullName "CreateExoPSSession.ps1"
. $scriptName
$null = Connect-EXOPSSession
$exchangeOnlineSession = (Get-PSSession | Where-Object { ($_.ConfigurationName -eq 'Microsoft.Exchange') -and ($_.State -eq 'Opened') })[0]
On lines 1 and 2, I’m getting the location of the different tools and libraries that we installed earlier. Once I find the ExoPowerShellModule.dll, I can import it like any other module, except I’m specifying the full path, on line 3.
Lines 4 and 5 are where I find and dot source CreateExoPSSession.ps1 which is the script that contains the Connect-EXOPSSession cmdlet (which I’d be remiss if I didn’t mention violates the PowerShell naming standards created by the community and advertised by Microsoft). That cmdlet will trigger a login process that includes MFA, similar to how Login-AzureRmAccount works.
Finally on lines 6 and 7, I’m creating a new session and then assigning it to a variable called $exchangeOnlineSession. Then I can import that session and I’ll be away to the races.
It’s not as convenient or straightforward as connecting without MFA, but it’s definitely safer.
Recommend
-
12
Using More Than One Multi-Factor Authentication Tool Yields Frictionless ResultsJanuary 29th 2021 new story
-
12
Connecting to Exchange Online PowerShell via client secret Microsoft
-
8
<?xml encoding="utf-8" ??>Introduction Two Factor Authentication, or 2FA, is a technique that adds a layer of security by requiring an additional authentication step to log in. For strong securi...
-
4
Not FoundYou just hit a route that doesn't exist... the sadness.LoginRadius empowers businesses to deliver a delightful customer experience and win customer trust. Using the LoginRadius Identity...
-
10
October 5, 2022 ...
-
10
Twitter Users Warn Others Using Two-Factor Authentication Not To Sign Out As 2FA Texts Aren't Arriving ...
-
5
Connecting to Exchange Online PowerShell by passing an access token December 2022 has certainly been an eventful month for Exchange Online PowerShell, with bunch of new improvements to cover, and the news of the
-
5
How to Secure Your Apple ID Using Two-Factor AuthenticationMonday February 5, 2018 10:59 AM PST by Tim HardwickAppl...
-
4
Two Factor Authentication with Web API and Angular using Google Authenticator Posted by Code Maze
-
4
Certificate-based authentication for Exchange Online Remote PowerShell Posted on
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK