Skip to content

Instantly share code, notes, and snippets.

@irwins
Last active August 15, 2017 04:54
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 irwins/3c25287c6d5e2a7cf8b34f8e27cab58d to your computer and use it in GitHub Desktop.
Save irwins/3c25287c6d5e2a7cf8b34f8e27cab58d to your computer and use it in GitHub Desktop.
#region Get Access token from Grap Explore Session
$accessToken = #Get the AccessTokenquick & Dirty using developers tool trick by Dan Silver https://twitter.com/dansilver82/status/872520061843415040
#endregion
#region Format request Header
$requestHeader = @{
"Authorization" = "Bearer " + $accessToken #For now get AccessToken from Developers tools console
"Content-Type" = "application/json"
}
#endregion
#region Get Users Tenant properties to query with Graph
$userProperties = @(
'displayName'
'GivenName'
'surName'
'department'
'officeLocation'
'jobTitle'
'userPrincipalName'
'id'
)
$iwrUsersParams = @{
Uri = "https://graph.microsoft.com/v1.0/users?`$select=$($userProperties -join ',')"
Method = 'Get'
Headers = $requestHeader
}
$iwrUsersResults = Invoke-WebRequest @iwrUsersParams
$iwrUsersObjects = $iwrUsersResults.Content | ConvertFrom-Json
$iwrUsersObjects.value
#endregion
#region Get Manager per Object ID
$iwrUsersObjects.value |
ForEach-Object {
try {
$obj = $_
$iwrManagerParams = @{
Uri = 'https://graph.microsoft.com/v1.0/users/{0}/manager' -f $obj.ID
Method = 'Get'
Headers = $requestHeader
}
$userManager = Invoke-WebRequest @iwrManagerParams
$iwrManagerObject = $userManager.Content | ConvertFrom-Json
[PSCustomObject]@{
ObjectID = $obj.Id
UserPrincipalName = $obj.userPrincipalName
DisplayName = $obj.DisplayName
GivenName = $obj.givenName
SurName = $obj.surname
JobTitle = $obj.jobTitle
Department = $obj.department
Manager = $iwrManagerObject.displayName
}
}
Catch {
[PSCustomObject]@{
ObjectID = $obj.Id
UserPrincipalName = $obj.userPrincipalName
DisplayName = $obj.DisplayName
GivenName = $obj.givenName
SurName = $obj.surname
JobTitle = $obj.jobTitle
Department = $obj.department
Manager = $null
}
}
}
#endregion
#region Get Access token from Grap Explore Session
$accessToken = $null
#endregion
#region Format request Header
$requestHeader = @{
"Authorization" = "Bearer " + $Accesstoken #For now get AccessToken from Developers tools console
"Content-Type" = "application/json"
}
#endregion
$iwrUsersParams = @{
Uri = "https://graph.microsoft.com/beta/users?`$expand=Manager"
Method = 'Get'
Headers = $requestHeader
}
$iwmUserResults = Invoke-RestMethod @iwrUsersParams
$iwmUserResults.value |
ForEach-Object {
[PSCustomObject]@{
DisplayName = $_.displayname
GivenName = $_.givenname
SurName = $_.surname
JobTitle = $_.jobtitle
Department = $_.department
Manager = $_.manager.displayname
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment