Skip to content

Instantly share code, notes, and snippets.

@gscales
Created July 5, 2020 10:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gscales/1b7973f6fe194804270c9934a887b705 to your computer and use it in GitHub Desktop.
Save gscales/1b7973f6fe194804270c9934a887b705 to your computer and use it in GitHub Desktop.
MFA Exchange Online Powershell
Import-Module CredentialManager
Import-Module ./AzureMFAOTPv2.ps1
$Cred = Get-Credential
$token = Get-AccessTokenMFA -OTP (Get-TimeBasedOneTimePassword -SharedSecret (Get-StoredCredential -Target Auth.GS -AsCredentialObject).Password) -Credential $Cred -ClientId 'a0c73c16-a7e3-4564-9a95-2bdf47383716' -Scopes 'https://outlook.office365.com/.default'
$domain = $Cred.UserName.Split('@')[1]
$tokenValue = ConvertTo-SecureString "Bearer $($token.access_token)" -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($Cred.UserName, $tokenValue)
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://outlook.office365.com/powershell-liveid?DelegatedOrg=$($domain)&BasicAuthToOAuthConversion=true" -Credential $credential -Authentication Basic -AllowRedirection
Import-PSSession $session
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment