Skip to content

Instantly share code, notes, and snippets.

@altrive
Created March 29, 2014 17:31
Show Gist options
  • Save altrive/9858591 to your computer and use it in GitHub Desktop.
Save altrive/9858591 to your computer and use it in GitHub Desktop.
$ErrorActionPreference = 'Stop'
function Main
{
Use-NuGetPackage -PackageId Microsoft.IdentityModel.Clients.ActiveDirectory -Version 1.0.3 -Verbose #Require PSNuGet <https://github.com/altrive/PSNuGet>
#Azure AD native client application settings
$authority = 'https://login.windows.net/{0}.onmicrosoft.com' -f 'altrive' #Set AzureAD tenant
$clientId = '[Native Client Application ClientID]' #Native Client Application ClientID
$redirectUri = [Uri] 'http://localhost' #Dummy URL
#varidate authorith:$false
$authCtx = New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext($authority)
#Show login dialog and get token for AzureAD graph api
$token = $authCtx.AcquireToken("https://graph.windows.net", $clientId, $redirectUri)
$header = $token.CreateAuthorizationHeader()
$uri = [string]::Format('https://graph.windows.net/{0}/users?api-version=2013-11-08', $token.TenantId)
#Query AzureAD graph API
$result = Invoke-RestMethod -Method Get -Uri $uri -Headers @{ 'Authorization' = $header; 'Content-Type' = 'application/json' }
return $result.value
}
Main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment