Skip to content

Instantly share code, notes, and snippets.

View poiriersimon's full-sized avatar

Simon Poirier poiriersimon

View GitHub Profile
@poiriersimon
poiriersimon / TrapInvalidAuth.ps1
Created September 21, 2018 18:20
Sample Function to catch expired Token in Powershell ADAL and do a new request.
function TrapInvalidAuth {
Trap {
if($_ -like "*InvalidAuthenticationToken*"){
$authheader = GetAuthHeaders;Invoke-RestMethod -Uri $uri –Headers $authHeader –Method Get
}
}
Invoke-RestMethod -Uri $uri –Headers $authHeader –Method Get
}
function GetAuthHeaders
@poiriersimon
poiriersimon / Microsoft Graph API Example - App with Secret.ps1
Created September 21, 2018 17:39
Microsoft Graph API Powershell Example - App with Secret
#Require an AzureAD App with Microsoft Graph API and
# App Permission
# - Read mail in all mailboxes
# Impersonation Permission
# - Read user and shared mail
# Don't forget to grant permission
#Pre-reqs for REST API calls
$ClientID = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
$ClientSecret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX="
@poiriersimon
poiriersimon / Manage Office 365 API Example.ps1
Last active September 21, 2018 17:40
Sample Powershell Script to check the Health of Office 365 Environment with Office 365 Management API
#Create an Web Azure AD Application + Key
#Permission - Office 365 Management APIs (both App and Impersonation)
# - Read service health information for your organization
# - Read activity data for your organization
# Don't forget to click on Grant Permission
# Based on https://github.com/OfficeDev/O365-InvestigationTooling/blob/master/O365InvestigationDataAcquisition.ps1
#Pre-reqs for REST API calls
$ClientID = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
$ClientSecret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
@poiriersimon
poiriersimon / GraphAPI Example.ps1
Created September 21, 2018 17:36
Sample Powershell script to connect and use Microsoft Graph API
#You need AzureAD Module (Save-Module AzureAD -Path C:\temp)
function GetAuthHeaders
{
[cmdletbinding()]
param(
[Parameter(Mandatory = $true)]
[string]$Tenant = "",
[Parameter(Mandatory = $true)]
[string]$UserPrincipalName = ""
)
@poiriersimon
poiriersimon / ConnectEXOWithToken.ps1
Created September 21, 2018 17:34
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
@poiriersimon
poiriersimon / Office 365 EXO EWS with Cert Auth API Example.ps1
Last active March 26, 2025 05:50
Exchange Online EWS with Certificate Authentication
#For this example you need an Azure App registered with a Self Sign Cert and a user with EWS Access to grab email.
#You need AzureAD Module (Save-Module AzureAD -Path C:\temp)
#You need EWS API 2.2 (www.microsoft.com/en-us/download/details.aspx?id=35371)
#App need to have Office 365 API access to read email.
# Permission under Office 365 Exchange Online:
# Admin : Use Exchange Web Services with full access to all mailboxes
# Delegate : Delegated permissions: full_access_as_user – Access mailbox as signed in user via Exc…
# Cert : https://github.com/Azure-Samples/active-directory-dotnet-daemon-certificate-credential/blob/master/Manual-Configuration-Steps.md
<#
$cert=New-SelfSignedCertificate -Subject "CN=Office365APIDemo" -CertStoreLocation "Cert:\CurrentUser\My" -KeyExportPolicy Exportable -KeySpec Signature