Skip to content

Instantly share code, notes, and snippets.

@michevnew
Created August 1, 2020 09:45
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 michevnew/029ee0bec0baaefcbf5556b32e108542 to your computer and use it in GitHub Desktop.
Save michevnew/029ee0bec0baaefcbf5556b32e108542 to your computer and use it in GitHub Desktop.
$tenantID = "tenant.onmicrosoft.com" #your tenantID or tenant root domain
$appID = "12345678-1234-1234-1234-1234567890AB" #the GUID of your app
$client_secret = "XXXXXXXXXXXXXXXXXXXXXX" #client secret for the app
$body = @{
client_id = $AppId
scope = "https://outlook.office365.com/.default"
client_secret = $client_secret
grant_type = "client_credentials"
}
$authenticationResult = Invoke-WebRequest -Method Post -Uri "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token" -ContentType "application/x-www-form-urlencoded" -Body $body -ErrorAction Stop
$token = ($authenticationResult.Content | ConvertFrom-Json).access_token
$Authorization = "Bearer {0}" -f $Token
$Password = ConvertTo-SecureString -AsPlainText $Authorization -Force
$Ctoken = New-Object System.Management.Automation.PSCredential -ArgumentList "OAuthUser@XXXXXXXXXXXXXXXXXX",$Password #replace your tenantGUID here
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://outlook.office365.com/PowerShell-LiveId?BasicAuthToOAuthConversion=true&email=user@tenant.onmicrosoft.com" -Credential $Ctoken -Authentication Basic -AllowRedirection -Verbose
Import-PSSession $Session
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment