Skip to content

Commit 72fb7bd

Browse files
committed
feat: enhance Intune compliance handling in Push-CIPPStandardsList function
1 parent 5156aa0 commit 72fb7bd

1 file changed

Lines changed: 34 additions & 4 deletions

File tree

Modules/CIPPCore/Public/Entrypoints/Activity Triggers/Standards/Push-CIPPStandardsList.ps1

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,23 @@ function Push-CIPPStandardsList {
126126

127127
# Filter unchanged templates
128128
$TemplateTable = Get-CippTable -tablename 'templates'
129-
$StandardTemplateTable = Get-CippTable -tablename 'templates'
130129
$IntuneKeys = @($ComputedStandards.Keys | Where-Object { $_ -like '*IntuneTemplate*' })
131130

131+
# Build compliance lookup - keyed by "standards.IntuneTemplate.<templateValue>"
132+
$IntuneComplianceLookup = @{}
133+
try {
134+
$AlignmentResults = Get-CIPPTenantAlignment -TenantFilter $TenantFilter
135+
foreach ($AlignmentResult in $AlignmentResults) {
136+
foreach ($Detail in $AlignmentResult.ComparisonDetails) {
137+
if ($Detail.StandardName -like 'standards.IntuneTemplate.*') {
138+
$IntuneComplianceLookup[$Detail.StandardName] = $Detail.Compliant
139+
}
140+
}
141+
}
142+
} catch {
143+
Write-Warning "Failed to get tenant alignment data for $TenantFilter : $($_.Exception.Message)"
144+
}
145+
132146
foreach ($Key in $IntuneKeys) {
133147
$Template = $ComputedStandards[$Key]
134148
$TemplateEntity = Get-CIPPAzDataTableEntity @TemplateTable -Filter "PartitionKey eq 'IntuneTemplate' and RowKey eq '$($Template.Settings.TemplateList.value)'"
@@ -168,10 +182,26 @@ function Push-CIPPStandardsList {
168182
} -Force | Out-Null
169183
}
170184

171-
# Remove if both unchanged
185+
# Remove or downgrade based on change state and compliance
172186
if (-not $PolicyChanged -and -not $StandardTemplateChanged) {
173-
Write-Host "NO INTUNE CHANGE: Filtering out $key for $($TenantFilter)"
174-
[void]$ComputedStandards.Remove($Key)
187+
$AlignmentKey = "standards.IntuneTemplate.$($Template.Settings.TemplateList.value)"
188+
$IsDeployed = $IntuneComplianceLookup.ContainsKey($AlignmentKey)
189+
$IsCompliant = $IsDeployed -and ($IntuneComplianceLookup[$AlignmentKey] -eq $true)
190+
191+
if ($IsCompliant) {
192+
# Policy unchanged and compliant - no action needed
193+
Write-Host "NO INTUNE CHANGE: Filtering out $Key for $TenantFilter (compliant)"
194+
[void]$ComputedStandards.Remove($Key)
195+
} elseif ($IsDeployed) {
196+
# Policy deployed but drifted, and nothing changed - report only, don't force remediate
197+
Write-Host "COMPLIANCE DRIFT: Downgrading $Key to Report mode for $TenantFilter (deployed, not compliant, no changes)"
198+
$ComputedStandards[$Key].Settings | Add-Member -NotePropertyName 'remediate' -NotePropertyValue $false -Force
199+
$ComputedStandards[$Key].Settings | Add-Member -NotePropertyName 'report' -NotePropertyValue $true -Force
200+
} else {
201+
# No alignment data yet - policy not yet deployed, skip (will run on next cycle with changes)
202+
Write-Host "NO INTUNE CHANGE: Filtering out $Key for $TenantFilter (not yet deployed, no changes)"
203+
[void]$ComputedStandards.Remove($Key)
204+
}
175205
}
176206
}
177207
} catch {

0 commit comments

Comments
 (0)