-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSend-GraphBatchRequest.psm1
More file actions
454 lines (380 loc) · 17.5 KB
/
Send-GraphBatchRequest.psm1
File metadata and controls
454 lines (380 loc) · 17.5 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
<#
.SYNOPSIS
Sends a batch request to Microsoft Graph API.
.DESCRIPTION
The Send-GraphBatchRequest function sends multiple Microsoft Graph API requests as a batch.
It supports automatic throttling handling, pagination, and can return results in either
PowerShell object format or raw JSON.
.PARAMETER AccessToken
The OAuth access token to authenticate against Microsoft Graph API.
.PARAMETER Requests
An array of request objects formatted for Microsoft Graph batch requests.
.PARAMETER DebugMode
Enables verbose debug logging to provide additional information about request processing.
.PARAMETER VerboseMode
Enables verbose output to give some information about the amount of sent requests.
.PARAMETER UserAgent
Specifies the user agent string to be used in the HTTP requests. This can be customized to mimic specific browser or application behavior.
Default: `python-requests/2.32.3`
.PARAMETER MaxRetries
Specifies the maximum number of retry attempts for failed requests. Default is 6.
.PARAMETER BetaAPI
If specified, uses the Graph Beta endpoint instead of v1.0.
.PARAMETER RawJson
If specified, returns the response as a raw JSON string instead of a PowerShell object.
.PARAMETER BatchDelay
Specifies a delay in seconds between each batch request to avoid throttling. Default is 0 (no delay).
.PARAMETER Proxy
Specifies a web proxy to use for the HTTP request (e.g., http://proxyserver:8080). Useful for debugging, traffic inspection.
.PARAMETER SkipCertificateCheck
If specified, skips TLS certificate validation for Invoke-RestMethod calls (PS 7).
.PARAMETER JsonDepthRequest
Specifies the depth for JSON conversion in the request. Default is 10, but can be increased for complex objects.
.PARAMETER QueryParameters
A hashtable of query parameters (e.g., @{ '$select' = 'displayName'; '$top' = '5' }) applied to all requests.
Individual requests can override or add their own query parameters by including a `queryParameters` hashtable in the request object.
.PARAMETER JsonDepthResponse
Specifies the depth for JSON conversion in the response (to use with -RawJson). Default is 10, but can be increased for complex objects.
.PARAMETER Silent
Suppresses error output (for example, when a sub-request returns an HTTP 400 error).
.PARAMETER DisablePagination
If specified, prevents the function from automatically following @odata.nextLink for paginated results.
.EXAMPLE
$AccessToken = "YOUR_ACCESS_TOKEN"
$Requests = @(
@{ "id" = "1"; "method" = "GET"; "url" = "/groups" }
)
Send-GraphBatchRequest -AccessToken $AccessToken -Requests $Requests -DebugMode
.EXAMPLE
$AccessToken = "YOUR_ACCESS_TOKEN"
$Requests = @(
@{
"id" = "1"
"method" = "POST"
"url" = "/groups"
"body" = @{ "displayName" = "New Group"; "mailEnabled" = $false; "mailNickname" = "whatever"; "securityEnabled" = $true }
"headers" = @{"Content-Type"= "application/json"}
}
)
Send-GraphBatchRequest -AccessToken $AccessToken -Requests $Requests -RawJson
.EXAMPLE
$AccessToken = "YOUR_ACCESS_TOKEN"
$Requests = @(
@{ "id" = "1"; "method" = "GET"; "url" = "/groups"},
@{"id" = "2"; "method" = "GET"; "url" = "/users"}
)
Send-GraphBatchRequest -AccessToken $AccessToken -Requests $Requests -DebugMode -proxy http://127.0.0.1:8080 -QueryParameters @{'$select' = 'displayName' }
.EXAMPLE
$AccessToken = "YOUR_ACCESS_TOKEN"
$Requests = @(
@{ id = "1"; method = "GET"; url = "/users"; queryParameters = @{ '$filter' = "startswith(displayName,'Adele')"; '$select' = 'displayName' } },
@{ id = "2"; method = "GET"; url = "/groups"; queryParameters = @{ '$select' = 'id' } }
)
Send-GraphBatchRequest -AccessToken $AccessToken -Requests $Requests
.NOTES
Author: ZH54321
GitHub: https://github.com/zh54321/GraphBatchRequest
#>
function Send-GraphBatchRequest {
param (
[Parameter(Mandatory = $true)]
[string]$AccessToken,
[Parameter(Mandatory = $true)]
[array]$Requests,
[int]$MaxRetries = 6,
[int]$JsonDepthRequest = 10,
[int]$JsonDepthResponse = 10,
[string]$UserAgent = "Mozilla/5.0 (Windows NT 10.0; Microsoft Windows 10.0.19045; en-us) PowerShell/7.5.0",
[double]$BatchDelay = 0,
[string]$Proxy,
[switch]$SkipCertificateCheck,
[hashtable]$QueryParameters,
[switch]$DebugMode,
[switch]$VerboseMode,
[switch]$Silent,
[switch]$BetaAPI,
[switch]$DisablePagination,
[switch]$RawJson
)
$ApiVersion = if ($BetaAPI) { "beta" } else { "v1.0" }
$BatchUrl = "https://graph.microsoft.com/$ApiVersion/`$batch"
$MaxBatchSize = 20
$HttpRequestCount = 0
$SubRequestCount = 0
$SupportsSkipCertificateCheck = (Get-Command Invoke-RestMethod).Parameters.ContainsKey('SkipCertificateCheck')
if (-not $Requests -or $Requests.Count -eq 0) {
Write-Error "No requests provided."
return
}
if ($SkipCertificateCheck -and -not $SupportsSkipCertificateCheck -and -not $Silent) {
Write-Warning "Current PowerShell does not support -SkipCertificateCheck on Invoke-RestMethod. The flag will be ignored."
}
$Batches = New-Object 'System.Collections.Generic.List[object]'
for ($i = 0; $i -lt $Requests.Count; $i += $MaxBatchSize) {
$Batches.Add($Requests[$i..([math]::Min($i + $MaxBatchSize - 1, $Requests.Count - 1))])
}
$Results = New-Object 'System.Collections.Generic.List[object]'
$GlobalNextLinks = New-Object 'System.Collections.Generic.List[string]'
$PagedResultsMap = @{}
foreach ($Batch in $Batches) {
$PendingRequests = $Batch
$RetryCount = 0
$RetryableStatusCodes = @(429, 500, 502, 503, 504)
$LastRetryableErrors = @{}
foreach ($req in $PendingRequests) {
$effectiveParams = @{}
if ($req.ContainsKey('queryParameters')) {
$effectiveParams += $req.queryParameters
}
if ($QueryParameters) {
foreach ($key in $QueryParameters.Keys) {
if (-not $effectiveParams.ContainsKey($key)) {
$effectiveParams[$key] = $QueryParameters[$key]
}
}
}
if ($effectiveParams.Count -gt 0) {
$queryString = ($effectiveParams.GetEnumerator() | ForEach-Object {
"$($_.Key)=$([uri]::EscapeDataString($_.Value))"
}) -join '&'
if ($req.url -notmatch "\?") {
$req.url = "$($req.url)?$queryString"
} else {
$req.url = "$($req.url)&$queryString"
}
}
}
do {
$BatchRequest = @{ requests = $PendingRequests }
$Headers = @{
"User-Agent" = $UserAgent
"Authorization" = "Bearer $AccessToken"
"Content-Type" = "application/json"
}
$irmParams = @{
Uri = $BatchUrl
Method = 'POST'
Headers = $Headers
Body = ($BatchRequest | ConvertTo-Json -Depth $JsonDepthRequest)
ErrorAction = 'Stop'
}
if ($Proxy) { $irmParams['Proxy'] = $Proxy }
if ($SkipCertificateCheck -and $SupportsSkipCertificateCheck) { $irmParams['SkipCertificateCheck'] = $true }
$HttpRequestCount++
$SubRequestCount += $PendingRequests.Count
try {
$Response = Invoke-RestMethod @irmParams
$PendingRequests = @()
} catch {
Write-Error "Batch request failed: $_"
return
}
$FailedRequests = @()
$RetryDelaySeconds = [math]::Pow(2, $RetryCount)
foreach ($Resp in $Response.responses) {
if ($Resp.status -ge 200 -and $Resp.status -lt 300) {
$ResultData = $Resp.body
$PagedResultsMap[$Resp.id] = New-Object 'System.Collections.Generic.List[object]'
if ($ResultData.value) {
$ResultData.value | ForEach-Object { $PagedResultsMap[$Resp.id].Add($_) }
}
if (-not $DisablePagination -and $ResultData.'@odata.nextLink') {
$GlobalNextLinks.Add("$($Resp.id)|$($ResultData.'@odata.nextLink')")
}
} else {
$ErrorCode = $Resp.body.error.code
$ErrorMessage = $Resp.body.error.message
if ($Resp.status -in $RetryableStatusCodes) {
$FailedRequests += $Batch | Where-Object { $_.id -eq $Resp.id }
$LastRetryableErrors[$Resp.id] = @{
status = $Resp.status
errorCode = $ErrorCode
errorMessage = $ErrorMessage
}
if (-not $Silent) {
if ($Resp.status -eq 429) {
Write-Host ("[i] Request ID {0} was throttled (429). Retrying automatically in {1}s (attempt {2}/{3}). No action needed." -f $Resp.id, $RetryDelaySeconds, ($RetryCount + 1), $MaxRetries)
} else {
Write-Host ("[i] Request ID {0} hit a temporary Graph error ({1}). Retrying automatically in {2}s (attempt {3}/{4})." -f $Resp.id, $Resp.status, $RetryDelaySeconds, ($RetryCount + 1), $MaxRetries)
}
}
} else {
if (-not $Silent) {
Write-Host "[!] Graph Batch Request: ID $($Resp.id) failed with status $($Resp.status): $ErrorCode - $ErrorMessage"
}
$Results.Add(@{ id = $Resp.id; status = $Resp.status; errorCode = $ErrorCode; errorMessage = $ErrorMessage })
}
}
}
$PendingRequests = $FailedRequests
if ($PendingRequests.Count -gt 0 -and ($RetryCount + 1) -lt $MaxRetries) {
Start-Sleep -Seconds $RetryDelaySeconds
}
$RetryCount++
} while ($PendingRequests.Count -gt 0 -and $RetryCount -lt $MaxRetries)
if ($PendingRequests.Count -gt 0) {
foreach ($PendingRequest in $PendingRequests) {
$RequestId = $PendingRequest.id
$LastError = $LastRetryableErrors[$RequestId]
$LastStatus = if ($null -ne $LastError) { $LastError.status } else { "unknown" }
$LastErrorCode = if ($null -ne $LastError) { $LastError.errorCode } else { $null }
$LastErrorMessage = if ($null -ne $LastError) { $LastError.errorMessage } else { "Retry attempts exhausted." }
if (-not $Silent) {
if ($LastStatus -eq 429) {
Write-Warning ("[!] Request ID {0} remained throttled after {1} retries." -f $RequestId, $MaxRetries)
} else {
Write-Warning ("[!] Request ID {0} still failed with status {1} after {2} retries: {3} - {4}" -f $RequestId, $LastStatus, $MaxRetries, $LastErrorCode, $LastErrorMessage)
}
}
$Results.Add(@{
id = $RequestId
status = $LastStatus
errorCode = $LastErrorCode
errorMessage = $LastErrorMessage
})
}
}
if ($BatchDelay -gt 0) {
Start-Sleep -Seconds $BatchDelay
}
}
while (-not $DisablePagination -and $GlobalNextLinks.Count -gt 0) {
$ToFetch = $GlobalNextLinks[0..([math]::Min(19, $GlobalNextLinks.Count - 1))]
$GlobalNextLinks.RemoveRange(0, $ToFetch.Count)
$Links = $ToFetch | ForEach-Object { ($_ -split '\|')[1] }
$Ids = $ToFetch | ForEach-Object { ($_ -split '\|')[0] }
$BatchResult = Invoke-GraphNextLinkBatch -NextLinks $Links `
-Ids $Ids `
-AccessToken $AccessToken `
-UserAgent $UserAgent `
-JsonDepthRequest $JsonDepthRequest `
-JsonDepthResponse $JsonDepthResponse `
-Proxy $Proxy `
-VerboseMode:$VerboseMode `
-DebugMode:$DebugMode `
-SkipCertificateCheck:$SkipCertificateCheck `
-HttpRequestCount ([ref]$HttpRequestCount) `
-SubRequestCount ([ref]$SubRequestCount)`
-ApiVersion $ApiVersion
foreach ($id in $BatchResult.values.Keys) {
if (-not $PagedResultsMap.ContainsKey($id)) {
Write-Warning ("[{0}] [!] Missing first-page data for ID {1} - initializing empty list." -f (Get-Date -Format "HH:mm:ss"), $id)
$PagedResultsMap[$id] = New-Object 'System.Collections.Generic.List[object]'
}
$PagedResultsMap[$id].AddRange($BatchResult.values[$id])
if ($BatchResult.nextLinks.ContainsKey($id)) {
$GlobalNextLinks.Add("$id|$($BatchResult.nextLinks[$id])")
}
}
}
foreach ($id in $PagedResultsMap.Keys) {
$Results.Add(@{ id = $id; status = 200; response = @{ value = $PagedResultsMap[$id].ToArray() } })
}
if ($VerboseMode) {
Write-Host "[i] Total HTTP requests sent (including pagination): $HttpRequestCount"
Write-Host "[i] Total Graph subrequests sent (individual operations): $SubRequestCount"
}
if ($RawJson) {
return $Results | ConvertTo-Json -Depth $JsonDepthResponse
} else {
return $Results
}
}
function Invoke-GraphNextLinkBatch {
param (
[string[]]$NextLinks,
[string[]]$Ids,
[string]$AccessToken,
[string]$UserAgent,
[int]$JsonDepthResponse = 10,
[int]$JsonDepthRequest = 10,
[string]$Proxy,
[switch]$SkipCertificateCheck,
[ref]$HttpRequestCount,
[ref]$SubRequestCount,
[switch]$VerboseMode,
[switch]$DebugMode,
[string]$ApiVersion
)
$ResultsList = New-Object 'System.Collections.Generic.List[object]'
$MoreNextLinks = New-Object 'System.Collections.Generic.List[string]'
$Headers = @{
"Authorization" = "Bearer $AccessToken"
"User-Agent" = $UserAgent
"Content-Type" = "application/json"
}
$SupportsSkipCertificateCheck = (Get-Command Invoke-RestMethod).Parameters.ContainsKey('SkipCertificateCheck')
for ($i = 0; $i -lt $NextLinks.Count; $i += 20) {
$BatchSet = $NextLinks[$i..([math]::Min($i + 19, $NextLinks.Count - 1))]
$BatchRequests = @()
$index = 0
foreach ($link in $BatchSet) {
$relativeUrl = $link -replace '^https://graph\.microsoft\.com/[^/]+', ''
$BatchRequests += @{
id = "nl_$index"
method = "GET"
url = $relativeUrl
}
$index++
}
$BatchBody = @{ requests = $BatchRequests }
$irmParams = @{
Uri = "https://graph.microsoft.com/$ApiVersion/`$batch"
Method = 'POST'
Headers = $Headers
Body = ($BatchBody | ConvertTo-Json -Depth $JsonDepthRequest)
ErrorAction = 'Stop'
}
if ($Proxy) { $irmParams['Proxy'] = $Proxy }
if ($SkipCertificateCheck -and $SupportsSkipCertificateCheck) { $irmParams['SkipCertificateCheck'] = $true }
try {
if ($DebugMode) { Write-Host "[i] Sending nextLink batch request..." }
$HttpRequestCount.Value++
$SubRequestCount.Value += $BatchRequests.Count
$BatchResp = Invoke-RestMethod @irmParams
$AllValues = New-Object 'System.Collections.Generic.List[object]'
$AllNextLinks = New-Object 'System.Collections.Generic.List[string]'
foreach ($resp in $BatchResp.responses) {
if ($resp.status -ge 200 -and $resp.status -lt 300) {
$data = $resp.body
# Ensure each slot is an array (even if null)
if ($data.value) {
$AllValues.Add(@($data.value))
} else {
$AllValues.Add(@())
}
if ($data.'@odata.nextLink') {
$AllNextLinks.Add($data.'@odata.nextLink')
} else {
$AllNextLinks.Add($null)
}
} else {
Write-Warning "NextLink subrequest failed: ID $($resp.id) ($($resp.status))"
$AllValues.Add(@()) # Maintain index consistency
$AllNextLinks.Add($null)
}
}
} catch {
Write-Error "Failed nextLink batch: $_ "
}
}
$ResultMap = @{}
$MoreLinksMap = @{}
foreach ($resp in $BatchResp.responses) {
$i = [int]($resp.id -replace 'nl_', '')
$realId = $Ids[$i]
if (-not $ResultMap.ContainsKey($realId)) {
$ResultMap[$realId] = New-Object 'System.Collections.Generic.List[object]'
}
if ($resp.body.value) {
$ResultMap[$realId].AddRange(@($resp.body.value))
}
if ($resp.body.'@odata.nextLink') {
$MoreLinksMap[$realId] = $resp.body.'@odata.nextLink'
}
}
return @{
values = $ResultMap
nextLinks = $MoreLinksMap
}
}