Skip to content

Instantly share code, notes, and snippets.

@poiriersimon
Created September 21, 2018 17:34
Show Gist options
  • Save poiriersimon/b84a9a78c49dae8784d9f02f7920b497 to your computer and use it in GitHub Desktop.
Save poiriersimon/b84a9a78c49dae8784d9f02f7920b497 to your computer and use it in GitHub Desktop.
Connect to Exchange Online Powershell ADAL without Click-to-Run
#Ref : https://www.michev.info/Blog/Post/1771/hacking-your-way-around-modern-authentication-and-the-powershell-modules-for-office-365
$TenantName = "TENANTNAME.onmicrosoft.com"
$UserPrincipalName = "[email protected]"
$resourceUri = "https://outlook.office365.com"
$redirectUri = "urn:ietf:wg:oauth:2.0:oob"
$clientid = "a0c73c16-a7e3-4564-9a95-2bdf47383716"
#You need AzureAD Module (Save-Module AzureAD -Path C:\temp)
Add-Type -Path C:\Temp\AzureAD\Microsoft.IdentityModel.Clients.ActiveDirectory.dll
$authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList https://login.microsoftonline.com/$($TenantName)/
$PromptBehavior = [Microsoft.IdentityModel.Clients.ActiveDirectory.PromptBehavior]::Auto
$platformParam = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.PlatformParameters" -ArgumentList $PromptBehavior
$userId = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.UserIdentifier" -ArgumentList $UserPrincipalName, "OptionalDisplayableId"
$authResult = $authContext.AcquireTokenAsync($resourceUri, $clientId, $redirectUri, $platformParam, $userId)
$Authorization = "Bearer {0}" -f $authResult.Result.AccessToken
$Password = ConvertTo-SecureString -AsPlainText $Authorization -Force
$Ctoken = New-Object System.Management.Automation.PSCredential -ArgumentList $UserPrincipalName, $Password
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/PowerShell-LiveId?BasicAuthToOAuthConversion=true -Credential $Ctoken -Authentication Basic -AllowRedirection
Import-PSSession $Session
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment