-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGraphApiOdataNextLink.ps1
More file actions
55 lines (40 loc) · 1.46 KB
/
Copy pathGraphApiOdataNextLink.ps1
File metadata and controls
55 lines (40 loc) · 1.46 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<#
.SYNOPSIS
Retrieves all Azure AD groups and devices using Microsoft Graph API with support for @odata.nextLink paging.
GitHub Repository: https://github.com/roalhelm/
.DESCRIPTION
This script connects to Microsoft Graph using the Microsoft Graph PowerShell SDK.
It retrieves all Azure AD groups and all devices by handling paginated results via the @odata.nextLink property.
The script demonstrates how to loop through paged Graph API results and aggregate all objects into arrays.
At the end, it outputs the total count of groups and devices retrieved.
.NOTES
Filename: GraphApiOdataNextLink.ps1
Requires: Microsoft.Graph PowerShell module
Permissions: Directory.Read.All or higher
.AUTHOR
Ronny Alhelm
.VERSION
1.0
.HISTORY
1.0 - Initial version
.EXAMPLE
.\GraphApiOdataNextLink.ps1
# Connects to Microsoft Graph, retrieves all groups and devices, and outputs their counts.
#>
Connect-MgGraph
$groupsUri = "https://graph.microsoft.com/beta/groups"
$allGroups = @()
do {
$response = Invoke-MgGraphRequest -Uri $groupsUri -Method Get
$allGroups += $response.value
$groupsUri = $response.'@odata.nextLink'
} while ($groupsUri)
$allGroups.Count
$devicesUri = "https://graph.microsoft.com/beta/devices"
$allDevices = @()
do {
$response = Invoke-MgGraphRequest -Uri $devicesUri -Method Get
$allDevices += $response.value
$devicesUri = $response.'@odata.nextLink'
} while ($devicesUri)
$allDevices.Count