Skip to content

Instantly share code, notes, and snippets.

@astaykov
Last active October 29, 2015 21:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save astaykov/316dd6ea1d5c83678b7f to your computer and use it in GitHub Desktop.
Save astaykov/316dd6ea1d5c83678b7f to your computer and use it in GitHub Desktop.
Function GetToken
{
param(
[String] $authority = "https://login.microsoftonline.com/<your_tenant>/oauth2/token",
[String] $clientId = "<client_id>",
[String] $clientSecret = "<client_secret>",
[String] $resourceId = "https%3a%2f%2fgraph.windows.net"
)
$body = "grant_type=client_credentials&resource=$($resourceId)&client_id=$($clientId)&client_secret=$($clientSecret)"
$res = Invoke-WebRequest -Uri $authority -Body $body -Method Post
$authResult = $res.Content | ConvertFrom-Json
return $authResult.access_token
}
Function GetUsersPage
{
param(
[String] $token,
[String] $nextLink = $null
)
if($nextLink -ne $null -and $nextLink.IndexOf('token=') -gt 0)
{
$nextLink = $nextLink.Substring($nextLink.IndexOf('token=') + 6)
}
$headers = @{"Authorization" = "Bearer $($aToken)"; "Content-Type" = "application/json"}
$usersUri = "https://graph.windows.net/lab09.onmicrosoft.com/users?api-version=1.5&`$top=2&`$skiptoken=$($nextLink)"
$usersResult = Invoke-WebRequest -Uri $usersUri -Headers $headers
$users = $usersResult.Content | ConvertFrom-Json
return $users
}
Function GetUsers
{
param(
[String] $token
)
$usersPage = GetUsersPage -token $token -nextLink $null
$usersPage.value
$nextLink = $usersPage.'odata.nextLink'
while($nextLink -ne $null)
{
$nextPage = GetUsersPage -token $token -nextLink $nextLink
$nextPage.value
$nextLink = $nextPage.'odata.nextLink'
}
}
$aToken = GetToken
$brr = GetUsers -token $aToken
$brr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment