Skip to content

Commit 58df931

Browse files
committed
cache license for faster dynamic tenant group processing
1 parent 78d2927 commit 58df931

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

Modules/CIPPCore/Public/TenantGroups/Update-CIPPDynamicTenantGroups.ps1

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ function Update-CIPPDynamicTenantGroups {
1717
try {
1818
$GroupTable = Get-CippTable -tablename 'TenantGroups'
1919
$MembersTable = Get-CippTable -tablename 'TenantGroupMembers'
20+
$LicenseCacheTable = Get-CippTable -tablename 'cachetenantskus'
21+
22+
$Skus = Get-CIPPAzDataTableEntity @LicenseCacheTable -Filter "PartitionKey eq 'sku' and Timestamp ge datetime'$( (Get-Date).ToUniversalTime().AddHours(-8).ToString('yyyy-MM-ddTHH:mm:ssZ') )'"
23+
24+
$SkuHashtable = @{}
25+
foreach ($Sku in $Skus) {
26+
if ($Sku.JSON -and (Test-Json -Json $Sku.JSON -ErrorAction SilentlyContinue)) {
27+
$SkuHashtable[$Sku.RowKey] = $Sku.JSON | ConvertFrom-Json
28+
}
29+
}
2030

2131
if ($GroupId) {
2232
$DynamicGroups = Get-CIPPAzDataTableEntity @GroupTable -Filter "PartitionKey eq 'TenantGroup' and RowKey eq '$GroupId'"
@@ -88,7 +98,20 @@ function Update-CIPPDynamicTenantGroups {
8898
}
8999
$TenantObj = $AllTenants | ForEach-Object {
90100
if ($Rules.property -contains 'availableLicense') {
91-
$LicenseInfo = New-GraphGetRequest -uri 'https://graph.microsoft.com/v1.0/subscribedSkus' -TenantId $_.defaultDomainName
101+
if ($SkuHashtable.ContainsKey($_.customerId)) {
102+
Write-Information "Using cached licenses for tenant $($_.defaultDomainName)"
103+
$LicenseInfo = $SkuHashtable[$_.customerId]
104+
} else {
105+
Write-Information "Fetching licenses for tenant $($_.defaultDomainName)"
106+
$LicenseInfo = New-GraphGetRequest -uri 'https://graph.microsoft.com/v1.0/subscribedSkus' -TenantId $_.defaultDomainName
107+
# Cache the result
108+
$CacheEntity = @{
109+
PartitionKey = 'sku'
110+
RowKey = [string]$_.customerId
111+
JSON = [string]($LicenseInfo | ConvertTo-Json -Depth 5 -Compress)
112+
}
113+
Add-CIPPAzDataTableEntity @LicenseCacheTable -Entity $CacheEntity -Force
114+
}
92115
}
93116
$SKUId = $LicenseInfo.SKUId ?? @()
94117
$ServicePlans = (Get-CIPPTenantCapabilities -TenantFilter $_.defaultDomainName).psobject.properties.name

0 commit comments

Comments
 (0)