Skip to content

Commit 6e39967

Browse files
committed
batch $expand support
limited use functionality (e.g. scheduler) due to the length of time this may take to complete
1 parent 850af4f commit 6e39967

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

Modules/CIPPCore/Public/GraphRequests/Get-GraphRequestList.ps1

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ function Get-GraphRequestList {
5454
.PARAMETER Caller
5555
Name of the calling function
5656
57+
.PARAMETER UseBatchExpand
58+
Perform a batch lookup using the $expand query parameter to avoid 20 item max
59+
5760
#>
5861
[CmdletBinding()]
5962
param(
@@ -76,7 +79,8 @@ function Get-GraphRequestList {
7679
[switch]$ReverseTenantLookup,
7780
[string]$ReverseTenantLookupProperty = 'tenantId',
7881
[boolean]$AsApp = $false,
79-
[string]$Caller = 'Get-GraphRequestList'
82+
[string]$Caller = 'Get-GraphRequestList',
83+
[switch]$UseBatchExpand
8084
)
8185

8286
$SingleTenantThreshold = 8000
@@ -109,7 +113,12 @@ function Get-GraphRequestList {
109113
} else {
110114
$Value = $Item.Value
111115
}
112-
$ParamCollection.Add($Item.Key, $Value)
116+
117+
if ($UseBatchExpand.IsPresent -and ($Item.Key -eq '$expand' -or $Item.Key -eq 'expand')) {
118+
$BatchExpandQuery = $Item.Value
119+
} else {
120+
$ParamCollection.Add($Item.Key, $Value)
121+
}
113122
}
114123
}
115124
$GraphQuery.Query = $ParamCollection.ToString()
@@ -331,6 +340,34 @@ function Get-GraphRequestList {
331340
$GraphRequestResults = New-GraphGetRequest @GraphRequest -Caller $Caller -ErrorAction Stop
332341
$GraphRequestResults = $GraphRequestResults | Select-Object *, @{n = 'Tenant'; e = { $TenantFilter } }, @{n = 'CippStatus'; e = { 'Good' } }
333342

343+
if ($UseBatchExpand.IsPresent -and ![string]::IsNullOrEmpty($BatchExpandQuery)) {
344+
if ($BatchExpandQuery -match '' -and ![string]::IsNullOrEmpty($GraphRequestResults.id)) {
345+
# Convert $expand format to actual batch query e.g. members($select=id,displayName) to members?$select=id,displayName
346+
$BatchExpandQuery = $BatchExpandQuery -replace '\(\$?([^=]+)=([^)]+)\)', '?$$$1=$2' -replace ';', '&'
347+
348+
# Extract property name from expand
349+
$Property = $BatchExpandQuery -replace '\?.*$', '' -replace '^.*\/', ''
350+
Write-Information "Performing batch expansion for property '$Property'..."
351+
352+
$Uri = "$Endpoint/{0}/$BatchExpandQuery"
353+
354+
$Requests = foreach ($Result in $GraphRequestResults) {
355+
@{
356+
id = $Result.id
357+
url = $Uri -f $Result.id
358+
method = 'GET'
359+
}
360+
}
361+
$BatchResults = New-GraphBulkRequest -Requests @($Requests) -tenantid $TenantFilter -NoAuthCheck $NoAuthCheck.IsPresent -asapp $AsApp
362+
363+
$GraphRequestResults = foreach ($Result in $GraphRequestResults) {
364+
$PropValue = $BatchResults | Where-Object { $_.id -eq $Result.id } | Select-Object -ExpandProperty body
365+
$Result | Add-Member -MemberType NoteProperty -Name $Property -Value ($PropValue.value ?? $PropValue)
366+
$Result
367+
}
368+
}
369+
}
370+
334371
if ($ReverseTenantLookup -and $GraphRequestResults) {
335372
$ReverseLookupRequests = $GraphRequestResults.$ReverseTenantLookupProperty | Sort-Object -Unique | ForEach-Object {
336373
@{

0 commit comments

Comments
 (0)