-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthenticate-managed-identity-in-runbook.ps1
36 lines (29 loc) · 1.27 KB
/
authenticate-managed-identity-in-runbook.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<#
.SYNOPSIS
This shows how you can obtain a access token from a managed identity running in a Azure Runbook.
.DESCRIPTION
This shows how you can obtain a access token from a managed identity running in a Azure Runbook or Azure Function.
This example uses the Powershell Module 'Microsoft.Graph.Authentication',
.NOTES
Author: Øyvind Nilsen ([email protected])
Creation Date: September 8th 2023
.LINK
https://github.com/NTNU-IT-M365/ms-graph-snippets
#>
#
# Example on how to obtain and use a Microsoft Graph API access token for a managed identity
#
$resourceURL = "https://graph.microsoft.com/"
$response = [System.Text.Encoding]::Default.GetString((Invoke-WebRequest -UseBasicParsing -Uri "$($env:IDENTITY_ENDPOINT)?resource=$resourceURL" -Method 'GET' -Headers @{'X-IDENTITY-HEADER' = "$env:IDENTITY_HEADER"; 'Metadata' = 'True'}).RawContentStream.ToArray()) | ConvertFrom-Json
#
# Example on how to use the access token to authenticate using Invoke-RestMethod
#
# Create a header containing the token
$headers = @{
Authorization="$($response.token_type) $($response.access_token)"
}
Invoke-RestMethod -Headers $headers -Uri "https://graph.microsoft.com/v1.0/organization"
#
# Example on authenticate a managed identity with MgGraph
#
Connect-MgGraph -Identity -NoWelcome