@@ -187,34 +187,59 @@ function Test-CIPPAuditLogRules {
187187 }
188188
189189 if (! $Lookups -or $NeedsRefresh ) {
190- # Collect bulk data for users/groups/devices/applications
191- $Requests = @ (
192- @ {
193- id = ' users'
194- url = ' /users?$select=id,displayName,userPrincipalName,accountEnabled&$top=999'
195- method = ' GET'
196- }
197- @ {
198- id = ' groups'
199- url = ' /groups?$select=id,displayName,mailEnabled,securityEnabled&$top=999'
200- method = ' GET'
201- }
202- @ {
203- id = ' devices'
204- url = ' /devices?$select=id,displayName,deviceId&$top=999'
205- method = ' GET'
206- }
207- @ {
208- id = ' servicePrincipals'
209- url = ' /servicePrincipals?$select=id,displayName&$top=999'
210- method = ' GET'
190+ # Try CippReportingDB first (pre-populated by timer, same pattern as Add-CIPPApplicationPermission)
191+ Write-Information " Checking CippReportingDB for directory data for tenant $TenantFilter "
192+ try {
193+ $Users = @ (New-CIPPDbRequest - TenantFilter $TenantFilter - Type ' Users' )
194+ $ServicePrincipals = @ (New-CIPPDbRequest - TenantFilter $TenantFilter - Type ' ServicePrincipals' )
195+ } catch {
196+ Write-Information " CippReportingDB query failed for ${TenantFilter} : $ ( $_.Exception.Message ) "
197+ $Users = @ ()
198+ $ServicePrincipals = @ ()
199+ }
200+
201+ if (! $Users -or ! $ServicePrincipals ) {
202+ # DB cache is empty or unavailable, fall back to Graph bulk request
203+ Write-Information " CippReportingDB has no data for $TenantFilter , falling back to Graph bulk request"
204+ $Requests = @ (
205+ @ {
206+ id = ' users'
207+ url = ' /users?$select=id,displayName,userPrincipalName,accountEnabled&$top=999'
208+ method = ' GET'
209+ }
210+ @ {
211+ id = ' groups'
212+ url = ' /groups?$select=id,displayName,mailEnabled,securityEnabled&$top=999'
213+ method = ' GET'
214+ }
215+ @ {
216+ id = ' devices'
217+ url = ' /devices?$select=id,displayName,deviceId&$top=999'
218+ method = ' GET'
219+ }
220+ @ {
221+ id = ' servicePrincipals'
222+ url = ' /servicePrincipals?$select=id,displayName&$top=999'
223+ method = ' GET'
224+ }
225+ )
226+ $Response = New-GraphBulkRequest - TenantId $TenantFilter - Requests $Requests
227+ $Users = ($Response | Where-Object { $_.id -eq ' users' }).body.value ?? @ ()
228+ $Groups = ($Response | Where-Object { $_.id -eq ' groups' }).body.value ?? @ ()
229+ $Devices = ($Response | Where-Object { $_.id -eq ' devices' }).body.value ?? @ ()
230+ $ServicePrincipals = ($Response | Where-Object { $_.id -eq ' servicePrincipals' }).body.value ?? @ ()
231+ $Response = $null
232+ } else {
233+ try {
234+ $Groups = @ (New-CIPPDbRequest - TenantFilter $TenantFilter - Type ' Groups' )
235+ $Devices = @ (New-CIPPDbRequest - TenantFilter $TenantFilter - Type ' Devices' )
236+ } catch {
237+ Write-Information " CippReportingDB Groups/Devices query failed for ${TenantFilter} : $ ( $_.Exception.Message ) "
238+ $Groups = @ ()
239+ $Devices = @ ()
211240 }
212- )
213- $Response = New-GraphBulkRequest - TenantId $TenantFilter - Requests $Requests
214- $Users = ($Response | Where-Object { $_.id -eq ' users' }).body.value ?? @ ()
215- $Groups = ($Response | Where-Object { $_.id -eq ' groups' }).body.value ?? @ ()
216- $Devices = ($Response | Where-Object { $_.id -eq ' devices' }).body.value ?? @ ()
217- $ServicePrincipals = ($Response | Where-Object { $_.id -eq ' servicePrincipals' }).body.value ?? @ ()
241+ Write-Information " Loaded from CippReportingDB: $ ( $Users.Count ) users, $ ( $Groups.Count ) groups, $ ( $Devices.Count ) devices, $ ( $ServicePrincipals.Count ) service principals"
242+ }
218243
219244 # Build hashtables for O(1) GUID lookups
220245 Write-Information " Building hashtable lookups for tenant $TenantFilter "
@@ -342,17 +367,28 @@ function Test-CIPPAuditLogRules {
342367 }
343368 }
344369
345- # partner users
346- $PartnerUsers = New-GraphGetRequest - uri " https://graph.microsoft.com/beta/users?`$ select=id,displayName,userPrincipalName,accountEnabled&`$ top=999" - AsApp $true - NoAuthCheck $true
347-
348- # Build partner user hashtable
349- $PartnerUserLookup = @ {}
350- foreach ($PartnerUser in $PartnerUsers ) {
351- if (! [string ]::IsNullOrEmpty($PartnerUser.id )) {
352- $PartnerUserLookup [$PartnerUser.id ] = $PartnerUser
370+ # Partner users - cache in cacheauditloglookups (PartitionKey '_partner') to avoid a fresh Graph fetch every invocation
371+ $PartnerUsersCache = Get-CIPPAzDataTableEntity @Table - Filter " PartitionKey eq '_partner' and RowKey eq 'users' and Timestamp gt datetime'$1dayago '"
372+ if ($PartnerUsersCache -and $PartnerUsersCache.Format -eq ' hashtable' ) {
373+ Write-Information ' Loading partner user hashtable from cache'
374+ $PartnerUserLookup = ($PartnerUsersCache.Data | ConvertFrom-Json - ErrorAction SilentlyContinue - AsHashtable) ?? @ {}
375+ } else {
376+ $PartnerUsers = New-GraphGetRequest - uri " https://graph.microsoft.com/beta/users?`$ select=id,displayName,userPrincipalName,accountEnabled&`$ top=999" - AsApp $true - NoAuthCheck $true
377+ $PartnerUserLookup = @ {}
378+ foreach ($PartnerUser in $PartnerUsers ) {
379+ if (! [string ]::IsNullOrEmpty($PartnerUser.id )) {
380+ $PartnerUserLookup [$PartnerUser.id ] = $PartnerUser
381+ }
353382 }
383+ Add-CIPPAzDataTableEntity @Table - Entity @ {
384+ PartitionKey = ' _partner'
385+ RowKey = ' users'
386+ Data = [string ]($PartnerUserLookup | ConvertTo-Json - Compress)
387+ Format = ' hashtable'
388+ } - Force
389+ $PartnerUsers = $null
354390 }
355- Write-Information " Built partner user hashtable: $ ( $PartnerUserLookup.Count ) partner users"
391+ Write-Information " Partner user hashtable: $ ( $PartnerUserLookup.Count ) partner users"
356392
357393 Write-Warning ' ## Audit Log Configuration ##'
358394 Write-Information ($Configuration | ConvertTo-Json - Depth 10 )
0 commit comments