Created
September 21, 2017 12:52
-
-
Save JanVidarElven/8886a39957b56af5f34c1acde5e01af7 to your computer and use it in GitHub Desktop.
AzureFunctionMSGraphMSI.ps1
This file contains 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 Managed Service Identity info from Azure Functions Application Settings | |
$msiEndpoint = $env:MSI_ENDPOINT | |
$msiSecret = $env:MSI_SECRET | |
Write-Output $msiEndpoint | |
Write-Output $msiSecret | |
# Specify URI and Token AuthN Request Parameters | |
$apiVersion = "2017-09-01" | |
$resourceURI = "https://graph.microsoft.com" | |
$tokenAuthURI = $msiEndpoint + "?resource=$resourceURI&api-version=$apiVersion" | |
# Authenticate with MSI and get Token | |
$tokenResponse = Invoke-RestMethod -Method Get -Headers @{"Secret"="$msiSecret"} -Uri $tokenAuthURI | |
# This response should give us a Bearer Token for later use in Graph API calls | |
$accessToken = $tokenResponse.access_token | |
Write-Output "Access Token" | |
Write-Output $accessToken | |
# $tokenResponse | Get-member | |
# All Users from a Department | |
$userlisttURI = "https://graph.microsoft.com/v1.0/users?`$filter=Department eq 'Seinfeld'" | |
# Get the User objects via an authenticated request to Graph API with the help of Bearer Token in authorization header | |
$graphResponseUsers = Invoke-RestMethod -Method Get -Uri $userlisttURI -Headers @{"Authorization"="Bearer $accessToken"} | |
# Loop through PowerShell object returned from Graph query | |
foreach ($user in $graphResponseUsers.value) | |
{ | |
Write-Output $user.DisplayName | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment