Skip to content

Commit 73d5ab2

Browse files
license check
1 parent 24ba12e commit 73d5ab2

File tree

1 file changed

+34
-12
lines changed

1 file changed

+34
-12
lines changed

Modules/CIPPCore/Public/Standards/Get-CIPPStandards.ps1

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ function Get-CIPPStandards {
33
[Parameter(Mandatory = $false)]
44
[string]$TenantFilter = 'allTenants',
55

6+
[Parameter(Mandatory = $false)]
7+
[switch]$LicenseChecks = $false,
8+
69
[Parameter(Mandatory = $false)]
710
[switch]$ListAllTenants,
811

@@ -20,16 +23,16 @@ function Get-CIPPStandards {
2023
$Table = Get-CippTable -tablename 'templates'
2124
$Filter = "PartitionKey eq 'StandardsTemplateV2'"
2225
$Templates = (Get-CIPPAzDataTableEntity @Table -Filter $Filter | Sort-Object TimeStamp).JSON |
23-
ForEach-Object {
24-
try {
25-
# Fix old "Action" => "action"
26-
$JSON = $_ -replace '"Action":', '"action":' -replace '"permissionlevel":', '"permissionLevel":'
27-
ConvertFrom-Json -InputObject $JSON -ErrorAction SilentlyContinue
28-
} catch {}
29-
} |
30-
Where-Object {
31-
$_.GUID -like $TemplateId -and $_.runManually -eq $runManually
32-
}
26+
ForEach-Object {
27+
try {
28+
# Fix old "Action" => "action"
29+
$JSON = $_ -replace '"Action":', '"action":' -replace '"permissionlevel":', '"permissionLevel":'
30+
ConvertFrom-Json -InputObject $JSON -ErrorAction SilentlyContinue
31+
} catch {}
32+
} |
33+
Where-Object {
34+
$_.GUID -like $TemplateId -and $_.runManually -eq $runManually
35+
}
3336

3437
# 1.5. Expand templates that contain TemplateList-Tags into multiple standards
3538
$ExpandedTemplates = foreach ($Template in $Templates) {
@@ -398,12 +401,31 @@ function Get-CIPPStandards {
398401
}
399402
}
400403
}
404+
# Checking if IntuneTemplates have a license and if the tenant meets it, if not, remove from the arr so we only do ONE license check instead of hundreds.
405+
$IntuneTemplateFound = $LicenseChecks.IsPresent -and ($ComputedStandards.Keys.Where({ $_ -like '*IntuneTemplate*' }, 'First').Count -gt 0)
406+
if ($IntuneTemplateFound) {
407+
$TestResult = Test-CIPPStandardLicense -StandardName 'IntuneTemplate_general' -TenantFilter $TenantName -RequiredCapabilities @('INTUNE_A', 'MDM_Services', 'EMS', 'SCCM', 'MICROSOFTINTUNEPLAN1')
408+
if (-not $TestResult) {
409+
$IntuneKeys = @($ComputedStandards.Keys | Where-Object { $_ -like '*IntuneTemplate*' })
410+
foreach ($Key in $IntuneKeys) {
411+
$TemplateKey = ($Key -split '\|', 2)[1]
412+
if ($TemplateKey) {
413+
Set-CIPPStandardsCompareField -FieldName "standards.IntuneTemplate.$TemplateKey" -FieldValue 'This tenant does not have the required license for this standard.' -Tenant $TenantName
414+
}
415+
}
416+
Write-Host "We're removing Intune templates as the correct license is not present for this standard. We do this to not run unneeded cycles. If you're reading this don't touch."
417+
foreach ($Key in $IntuneKeys) { [void]$ComputedStandards.Remove($Key) }
418+
#After the license check we're now going to do a compare for the remaining standards.
419+
#This compare works by bulk requesting the top=1 entries for intune policies inside of the tenant.
420+
#if the 'lastModified' timestamp is the same we skip processing this standard too, there's no need because the tenant hasn't changed anything.
421+
#However, we also check the timestamp of our standardTemplate, if that is newer than the cached lastModified timestamp we also process the standard again, because the template has changed.
422+
#If our cache for this tenant is blank, we also process the standard.
423+
}
424+
}
401425

402-
# Emit one object per unique (StandardName, TemplateList.value)
403426
foreach ($Key in $ComputedStandards.Keys) {
404427
$Standard = $ComputedStandards[$Key]
405428
$StandardName = $Key -replace '\|.*$', ''
406-
407429
# Preserve TemplateId before removing
408430
$PreservedTemplateId = $Standard.TemplateId
409431
$Standard.PSObject.Properties.Remove('TemplateId') | Out-Null

0 commit comments

Comments
 (0)