This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Get any existing contexts | |
$context = [Microsoft.Exchange.Management.ExoPowershellSnapin.ConnectionContextFactory]::GetAllConnectionContexts() | |
#Get an existing token from the cache | |
$context[0].TokenProvider.GetValidTokenFromCache("Get-Mailbox").AuthorizationHeader | |
#Or generate a new one | |
$context[0].TokenProvider.GetAccessToken() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$req = Invoke-MgGraphRequest -Uri "https://graph.microsoft.com/v1.0/me" -OutputType HttpResponseMessage | |
$authHeader = @{ | |
Authorization = "Bearer $($req.RequestMessage.Headers.Authorization.Parameter)" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#region Functions | |
function Get-Members { | |
param ( | |
[Parameter(Mandatory=$true)][string[]]$groupIds | |
) | |
$members = @() | |
foreach ($groupId in $groupIds) { | |
$uri = "https://graph.microsoft.com/beta/groups/$groupId/transitiveMembers?`$top=999&`$select=id" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Load the MSAL binaries | |
Add-Type -Path "C:\Program Files\WindowsPowerShell\Modules\MSAL\Microsoft.Identity.Client.dll" | |
#Leverage the ADIbizaUX app | |
$app = [Microsoft.Identity.Client.PublicClientApplicationBuilder]::Create("1950a258-227b-4e31-a9cf-717495945fc2").WithRedirectUri("urn:ietf:wg:oauth:2.0:oob").WithTenantId("tenant.onmicrosoft.com").Build() | |
#Set the scope | |
$Scopes = New-Object System.Collections.Generic.List[string] | |
$Scope = "74658136-14ec-4630-ad9b-26e160ff0fc6/.default" | |
$Scopes.Add($Scope) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Define variables | |
$tenantId = "tenant.onmicrosoft.com" | |
$appId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | |
$thumbprint = "2B12FD0A0BF106B1A1C2C3D70C7395CD111574B1" | |
$resource = "https://graph.microsoft.com" | |
$authUrl = "https://login.microsoftonline.com/$tenantId/oauth2/token" | |
# Create a certificate object | |
$cert = Get-Item -Path Cert:\CurrentUser\My\$thumbprint |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function GenerateFolderId ($mailbox,$folderName) { | |
$folderId = [System.Convert]::FromBase64String((Get-MailboxFolderStatistics $mailbox | ? { $_.Name -eq "$folderName" }).FolderId) | |
$encoding = [System.Text.Encoding]::GetEncoding("us-ascii") | |
$nibbler = $encoding.GetBytes("0123456789ABCDEF") | |
$indexIdBytes = New-Object byte[] 48; $indexIdIdx = 0; | |
$folderId | select -Skip 23 -First 24 | % { $indexIdBytes[$indexIdIdx++] = $nibbler[$_ -shr 4]; $indexIdBytes[$indexIdIdx++] = $nibbler[$_ -band 0xF] } | |
return $encoding.GetString($indexIdBytes) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Connect-MgGraph -Tenant tenant.onmicrosoft.com -Scopes User.ReadWrite.All | |
#Import the list of users, or generate it dynamically as needed | |
$users = Import-Csv .\Users-to-disable.csv | |
#$users = Get-MgUser -Filter "Department eq 'Marketing'" | |
foreach ($user in $users) { | |
Write-Verbose "Processing licenses for user $($user.UserPrincipalName)" | |
try { $user = Get-MgUser -UserId $user.UserPrincipalName -ErrorAction Stop } | |
catch { Write-Verbose "User $($user.UserPrincipalName) not found, skipping..." ; continue } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$users = Import-Csv .\Users-to-disable.csv | |
foreach ($user in $users) { | |
Write-Verbose "Processing licenses for user $($user.UserPrincipalName)" | |
try { $user = Get-AzureADUser -ObjectId $user.UserPrincipalName -ErrorAction Stop } | |
catch { continue } | |
$SKUs = @($user.AssignedLicenses) | |
if (!$SKUs) { Write-Verbose "No Licenses found for user $($user.UserPrincipalName), skipping..." ; continue } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Set the authentication details | |
$tenantID = "tenant.onmicrosoft.com" #your tenantID or tenant root domain | |
$appID = "12345678-1234-1234-1234-1234567890AB" #the GUID of your app. For best result, use app with Policy.Read.All and Policy.ReadWrite.ConditionalAccess scopes granted | |
$client_secret = "XXXXXXXXXXXXXXXxxxx" #client secret for the app | |
$body = @{ | |
client_id = $AppId | |
scope = "https://graph.microsoft.com/.default" | |
client_secret = $client_secret | |
grant_type = "client_credentials" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Set the authentication details | |
$tenantID = "tenant.onmicrosoft.com" #your tenantID or tenant root domain | |
$appID = "12345678-1234-1234-1234-1234567890AB" #the GUID of your app. For best result, use app with Policy.ReadWrite.Authorization scope granted. | |
$client_secret = "XXXXXXXXXXXXXXXxxxx" #client secret for the app | |
$body = @{ | |
client_id = $AppId | |
scope = "https://graph.microsoft.com/.default" | |
client_secret = $client_secret | |
grant_type = "client_credentials" |
NewerOlder