Skip to content

Commit 6e3741e

Browse files
authored
Merge pull request #590 from KelvinTegelaar/dev
[pull] dev from KelvinTegelaar:dev
2 parents 8df937b + 4465517 commit 6e3741e

File tree

3 files changed

+108
-190
lines changed

3 files changed

+108
-190
lines changed

Modules/CIPPCore/Public/Functions/Get-CIPPTenantAlignment.ps1

Lines changed: 2 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,11 @@ function Get-CIPPTenantAlignment {
2424
[Parameter(Mandatory = $false)]
2525
[string]$TemplateId
2626
)
27-
28-
# Initialize overall stopwatch
29-
$OverallStopwatch = [System.Diagnostics.Stopwatch]::StartNew()
30-
$SectionTimings = @{}
31-
32-
# Measure template table initialization
33-
$sw = [System.Diagnostics.Stopwatch]::StartNew()
3427
$TemplateTable = Get-CippTable -tablename 'templates'
3528
$TemplateFilter = "PartitionKey eq 'StandardsTemplateV2'"
36-
$sw.Stop()
37-
$SectionTimings['TemplateTableInit'] = $sw.ElapsedMilliseconds
38-
Write-Verbose "Template table initialization took: $($sw.ElapsedMilliseconds)ms"
29+
$TenantGroups = Get-TenantGroups
3930

4031
try {
41-
# Measure template loading
42-
$sw = [System.Diagnostics.Stopwatch]::StartNew()
4332
# Get all standard templates
4433
$Templates = (Get-CIPPAzDataTableEntity @TemplateTable -Filter $TemplateFilter) | ForEach-Object {
4534
$JSON = $_.JSON
@@ -55,18 +44,12 @@ function Get-CIPPTenantAlignment {
5544
$Data
5645
}
5746
}
58-
$sw.Stop()
59-
$SectionTimings['TemplateLoading'] = $sw.ElapsedMilliseconds
60-
Write-Verbose "Template loading took: $($sw.ElapsedMilliseconds)ms"
61-
Write-Information "Loaded $($Templates.Count) templates in $($sw.ElapsedMilliseconds)ms"
6247

6348
if (-not $Templates) {
6449
Write-Warning 'No templates found matching the criteria'
6550
return @()
6651
}
6752

68-
# Measure standards data retrieval
69-
$sw = [System.Diagnostics.Stopwatch]::StartNew()
7053
# Get standards comparison data
7154
$StandardsTable = Get-CippTable -TableName 'CippStandardsReports'
7255
#this if statement is to bring down performance when running scheduled checks, we have to revisit this to a better query due to the extreme size this can get.
@@ -76,13 +59,7 @@ function Get-CIPPTenantAlignment {
7659
$filter = "PartitionKey ne 'StandardReport' and PartitionKey ne ''"
7760
}
7861
$AllStandards = Get-CIPPAzDataTableEntity @StandardsTable -Filter $filter
79-
$sw.Stop()
80-
$SectionTimings['StandardsDataRetrieval'] = $sw.ElapsedMilliseconds
81-
Write-Verbose "Standards data retrieval took: $($sw.ElapsedMilliseconds)ms"
82-
Write-Information "Retrieved $($AllStandards.Count) standards records in $($sw.ElapsedMilliseconds)ms"
8362

84-
# Measure tenant filtering
85-
$sw = [System.Diagnostics.Stopwatch]::StartNew()
8663
# Filter by tenant if specified
8764
$Standards = if ($TenantFilter) {
8865
$AllStandards
@@ -91,12 +68,6 @@ function Get-CIPPTenantAlignment {
9168
$AllStandards | Where-Object { $_.PartitionKey -in $Tenants.defaultDomainName }
9269
}
9370
$TagTemplates = Get-CIPPAzDataTableEntity @TemplateTable
94-
$sw.Stop()
95-
$SectionTimings['TenantFiltering'] = $sw.ElapsedMilliseconds
96-
Write-Verbose "Tenant filtering and tag template loading took: $($sw.ElapsedMilliseconds)ms"
97-
98-
# Measure tenant data structure building
99-
$sw = [System.Diagnostics.Stopwatch]::StartNew()
10071
# Build tenant standards data structure
10172
$tenantData = @{}
10273
foreach ($Standard in $Standards) {
@@ -124,32 +95,16 @@ function Get-CIPPTenantAlignment {
12495
}
12596
}
12697
$TenantStandards = $tenantData
127-
$sw.Stop()
128-
$SectionTimings['TenantDataStructureBuilding'] = $sw.ElapsedMilliseconds
129-
Write-Verbose "Tenant data structure building took: $($sw.ElapsedMilliseconds)ms"
130-
Write-Information "Built data structure for $($tenantData.Count) tenants in $($sw.ElapsedMilliseconds)ms"
13198

13299
$Results = [System.Collections.Generic.List[object]]::new()
133100

134-
# Measure template processing
135-
$sw = [System.Diagnostics.Stopwatch]::StartNew()
136-
$TemplateProcessingCount = 0
137-
$TemplateSectionTimings = @{
138-
TenantScopeSetup = 0
139-
StandardsDataExtraction = 0
140-
StandardsSetBuilding = 0
141-
TenantComparison = 0
142-
}
143101
# Process each template against all tenants
144102
foreach ($Template in $Templates) {
145-
$TemplateProcessingCount++
146103
$TemplateStandards = $Template.standards
147104
if (-not $TemplateStandards) {
148105
continue
149106
}
150107

151-
# Measure tenant scope setup
152-
$swTemplate = [System.Diagnostics.Stopwatch]::StartNew()
153108
# Check if template has tenant assignments (scope)
154109
$TemplateAssignedTenants = @()
155110
$AppliestoAllTenants = $false
@@ -158,7 +113,7 @@ function Get-CIPPTenantAlignment {
158113
# Extract tenant values from the tenantFilter array
159114
$TenantValues = $Template.tenantFilter | ForEach-Object {
160115
if ($_.type -eq 'group') {
161-
(Get-TenantGroups -GroupId $_.value).members.defaultDomainName
116+
($TenantGroups | Where-Object -Property GroupName -EQ $_.value).Members.defaultDomainName
162117
} else {
163118
$_.value
164119
}
@@ -172,11 +127,7 @@ function Get-CIPPTenantAlignment {
172127
} else {
173128
$AppliestoAllTenants = $true
174129
}
175-
$swTemplate.Stop()
176-
$TemplateSectionTimings.TenantScopeSetup += $swTemplate.ElapsedMilliseconds
177130

178-
# Measure standards data extraction
179-
$swTemplate = [System.Diagnostics.Stopwatch]::StartNew()
180131
$StandardsData = foreach ($StandardKey in $TemplateStandards.PSObject.Properties.Name) {
181132
$StandardConfig = $TemplateStandards.$StandardKey
182133
$StandardId = "standards.$StandardKey"
@@ -246,11 +197,7 @@ function Get-CIPPTenantAlignment {
246197
}
247198
}
248199
}
249-
$swTemplate.Stop()
250-
$TemplateSectionTimings.StandardsDataExtraction += $swTemplate.ElapsedMilliseconds
251200

252-
# Measure standards set building
253-
$swTemplate = [System.Diagnostics.Stopwatch]::StartNew()
254201
$AllStandards = $StandardsData.StandardId
255202
$AllStandardsArray = @($AllStandards)
256203
$ReportingDisabledStandards = ($StandardsData | Where-Object { -not $_.ReportingEnabled }).StandardId
@@ -261,11 +208,7 @@ function Get-CIPPTenantAlignment {
261208
foreach ($item in $TemplateAssignedTenants) { [void]$set.Add($item) }
262209
$set
263210
} else { $null }
264-
$swTemplate.Stop()
265-
$TemplateSectionTimings.StandardsSetBuilding += $swTemplate.ElapsedMilliseconds
266211

267-
# Measure tenant comparison processing
268-
$swTemplate = [System.Diagnostics.Stopwatch]::StartNew()
269212
foreach ($TenantName in $TenantStandards.Keys) {
270213
# Check tenant scope with HashSet and cache tenant data
271214
if (-not $AppliestoAllTenants) {
@@ -388,34 +331,7 @@ function Get-CIPPTenantAlignment {
388331

389332
$Results.Add($Result)
390333
}
391-
$swTemplate.Stop()
392-
$TemplateSectionTimings.TenantComparison += $swTemplate.ElapsedMilliseconds
393-
}
394-
$sw.Stop()
395-
$SectionTimings['TemplateProcessing'] = $sw.ElapsedMilliseconds
396-
Write-Verbose "Template processing took: $($sw.ElapsedMilliseconds)ms for $TemplateProcessingCount templates"
397-
Write-Information "Processed $TemplateProcessingCount templates in $($sw.ElapsedMilliseconds)ms"
398-
399-
# Output template sub-section timings
400-
Write-Verbose 'Template processing breakdown:'
401-
Write-Information 'Template processing breakdown:'
402-
foreach ($Section in $TemplateSectionTimings.GetEnumerator() | Sort-Object Value -Descending) {
403-
$Percentage = if ($sw.ElapsedMilliseconds -gt 0) { [math]::Round(($Section.Value / $sw.ElapsedMilliseconds) * 100, 2) } else { 0 }
404-
Write-Verbose " $($Section.Key): $($Section.Value)ms ($Percentage%)"
405-
Write-Information " $($Section.Key): $($Section.Value)ms ($Percentage%)"
406-
}
407-
408-
# Output timing summary
409-
$OverallStopwatch.Stop()
410-
Write-Information '=== Get-CIPPTenantAlignment Performance Summary ==='
411-
Write-Information "Total execution time: $($OverallStopwatch.ElapsedMilliseconds)ms"
412-
Write-Verbose 'Section timings:'
413-
foreach ($Section in $SectionTimings.GetEnumerator() | Sort-Object Value -Descending) {
414-
$Percentage = [math]::Round(($Section.Value / $OverallStopwatch.ElapsedMilliseconds) * 100, 2)
415-
Write-Verbose " $($Section.Key): $($Section.Value)ms ($Percentage%)"
416-
Write-Information " $($Section.Key): $($Section.Value)ms ($Percentage%)"
417334
}
418-
Write-Information '========================================'
419335

420336
return $Results
421337
} catch {

Modules/CIPPCore/Public/Get-CIPPDrift.ps1

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,9 @@ function Get-CIPPDrift {
2929
[switch]$AllTenants
3030
)
3131

32-
# Initialize overall stopwatch
33-
$OverallStopwatch = [System.Diagnostics.Stopwatch]::StartNew()
34-
$SectionTimings = @{}
35-
36-
# Measure license capability checks
37-
$sw = [System.Diagnostics.Stopwatch]::StartNew()
3832
$IntuneCapable = Test-CIPPStandardLicense -StandardName 'IntuneTemplate_general' -TenantFilter $TenantFilter -RequiredCapabilities @('INTUNE_A', 'MDM_Services', 'EMS', 'SCCM', 'MICROSOFTINTUNEPLAN1')
3933
$ConditionalAccessCapable = Test-CIPPStandardLicense -StandardName 'ConditionalAccessTemplate_general' -TenantFilter $TenantFilter -RequiredCapabilities @('AAD_PREMIUM', 'AAD_PREMIUM_P2')
4034
$IntuneTable = Get-CippTable -tablename 'templates'
41-
$sw.Stop()
42-
$SectionTimings['LicenseChecks'] = $sw.ElapsedMilliseconds
43-
Write-Verbose "License checks took: $($sw.ElapsedMilliseconds)ms"
44-
45-
# Measure Intune template loading
46-
$sw = [System.Diagnostics.Stopwatch]::StartNew()
4735
if ($IntuneCapable) {
4836
$IntuneFilter = "PartitionKey eq 'IntuneTemplate'"
4937
$RawIntuneTemplates = (Get-CIPPAzDataTableEntity @IntuneTable -Filter $IntuneFilter)
@@ -61,12 +49,6 @@ function Get-CIPPDrift {
6149
}
6250
} | Sort-Object -Property displayName
6351
}
64-
$sw.Stop()
65-
$SectionTimings['IntuneTemplateLoading'] = $sw.ElapsedMilliseconds
66-
Write-Verbose "Intune template loading took: $($sw.ElapsedMilliseconds)ms"
67-
68-
# Measure CA template loading
69-
$sw = [System.Diagnostics.Stopwatch]::StartNew()
7052
# Load all CA templates
7153
if ($ConditionalAccessCapable) {
7254
$CAFilter = "PartitionKey eq 'CATemplate'"
@@ -81,25 +63,15 @@ function Get-CIPPDrift {
8163
}
8264
} | Sort-Object -Property displayName
8365
}
84-
$sw.Stop()
85-
$SectionTimings['CATemplateLoading'] = $sw.ElapsedMilliseconds
86-
Write-Verbose "CA template loading took: $($sw.ElapsedMilliseconds)ms"
8766

8867
try {
89-
# Measure alignment data retrieval
90-
$sw = [System.Diagnostics.Stopwatch]::StartNew()
9168
$AlignmentData = Get-CIPPTenantAlignment -TenantFilter $TenantFilter -TemplateId $TemplateId | Where-Object -Property standardType -EQ 'drift'
92-
$sw.Stop()
93-
$SectionTimings['AlignmentDataRetrieval'] = $sw.ElapsedMilliseconds
94-
Write-Verbose "Alignment data retrieval took: $($sw.ElapsedMilliseconds)ms"
9569

9670
if (-not $AlignmentData) {
9771
Write-Warning "No alignment data found for tenant $TenantFilter"
9872
return @()
9973
}
10074

101-
# Measure drift state loading
102-
$sw = [System.Diagnostics.Stopwatch]::StartNew()
10375
# Get existing drift states from the tenantDrift table
10476
$DriftTable = Get-CippTable -tablename 'tenantDrift'
10577
$DriftFilter = "PartitionKey eq '$TenantFilter'"
@@ -112,14 +84,9 @@ function Get-CIPPDrift {
11284
} catch {
11385
Write-Warning "Failed to get existing drift states: $($_.Exception.Message)"
11486
}
115-
$sw.Stop()
116-
$SectionTimings['DriftStateLoading'] = $sw.ElapsedMilliseconds
117-
Write-Verbose "Drift state loading took: $($sw.ElapsedMilliseconds)ms"
11887

11988
$Results = [System.Collections.Generic.List[object]]::new()
12089
foreach ($Alignment in $AlignmentData) {
121-
# Measure standards deviation processing
122-
$sw = [System.Diagnostics.Stopwatch]::StartNew()
12390
# Initialize deviation collections
12491
$StandardsDeviations = [System.Collections.Generic.List[object]]::new()
12592
$PolicyDeviations = [System.Collections.Generic.List[object]]::new()
@@ -172,12 +139,7 @@ function Get-CIPPDrift {
172139
}
173140
}
174141
}
175-
$sw.Stop()
176-
$SectionTimings['StandardsDeviationProcessing'] = $sw.ElapsedMilliseconds
177-
Write-Verbose "Standards deviation processing took: $($sw.ElapsedMilliseconds)ms"
178142

179-
# Measure Intune policy collection
180-
$sw = [System.Diagnostics.Stopwatch]::StartNew()
181143
# Perform full policy collection
182144
if ($IntuneCapable) {
183145
# Always get live data when not in AllTenants mode
@@ -248,12 +210,6 @@ function Get-CIPPDrift {
248210
Write-Warning "Failed to get Intune policies: $($_.Exception.Message)"
249211
}
250212
}
251-
$sw.Stop()
252-
$SectionTimings['IntunePolicyCollection'] = $sw.ElapsedMilliseconds
253-
Write-Verbose "Intune policy collection took: $($sw.ElapsedMilliseconds)ms"
254-
255-
# Measure CA policy collection
256-
$sw = [System.Diagnostics.Stopwatch]::StartNew()
257213
# Get Conditional Access policies
258214
if ($ConditionalAccessCapable) {
259215
try {
@@ -271,12 +227,7 @@ function Get-CIPPDrift {
271227
$TenantCAPolicies = @()
272228
}
273229
}
274-
$sw.Stop()
275-
$SectionTimings['CAPolicyCollection'] = $sw.ElapsedMilliseconds
276-
Write-Verbose "CA policy collection took: $($sw.ElapsedMilliseconds)ms"
277230

278-
# Measure template extraction
279-
$sw = [System.Diagnostics.Stopwatch]::StartNew()
280231
if ($Alignment.standardSettings) {
281232
if ($Alignment.standardSettings.IntuneTemplate) {
282233
$IntuneTemplateIds = $Alignment.standardSettings.IntuneTemplate.TemplateList | ForEach-Object { $_.value }
@@ -304,12 +255,7 @@ function Get-CIPPDrift {
304255
Write-Warning "Failed to get Intune templates: $($_.Exception.Message)"
305256
}
306257
}
307-
$sw.Stop()
308-
$SectionTimings['TemplateExtraction'] = $sw.ElapsedMilliseconds
309-
Write-Verbose "Template extraction took: $($sw.ElapsedMilliseconds)ms"
310258

311-
# Measure Intune policy deviation checking
312-
$sw = [System.Diagnostics.Stopwatch]::StartNew()
313259
# Check for extra Intune policies not in template
314260
foreach ($TenantPolicy in $TenantIntunePolicies) {
315261
$PolicyFound = $false
@@ -345,12 +291,7 @@ function Get-CIPPDrift {
345291
$PolicyDeviations.Add($PolicyDeviation)
346292
}
347293
}
348-
$sw.Stop()
349-
$SectionTimings['IntunePolicyDeviationCheck'] = $sw.ElapsedMilliseconds
350-
Write-Verbose "Intune policy deviation check took: $($sw.ElapsedMilliseconds)ms"
351294

352-
# Measure CA policy deviation checking
353-
$sw = [System.Diagnostics.Stopwatch]::StartNew()
354295
# Check for extra Conditional Access policies not in template
355296
foreach ($TenantCAPolicy in $TenantCAPolicies) {
356297
$PolicyFound = $false
@@ -380,9 +321,6 @@ function Get-CIPPDrift {
380321
$PolicyDeviations.Add($PolicyDeviation)
381322
}
382323
}
383-
$sw.Stop()
384-
$SectionTimings['CAPolicyDeviationCheck'] = $sw.ElapsedMilliseconds
385-
Write-Verbose "CA policy deviation check took: $($sw.ElapsedMilliseconds)ms"
386324

387325

388326
# Combine all deviations and filter by status
@@ -422,17 +360,6 @@ function Get-CIPPDrift {
422360
$Results.Add($Result)
423361
}
424362

425-
# Output timing summary
426-
$OverallStopwatch.Stop()
427-
Write-Information '=== Get-CIPPDrift Performance Summary ==='
428-
Write-Information "Total execution time: $($OverallStopwatch.ElapsedMilliseconds)ms"
429-
Write-Information "`nSection timings:"
430-
foreach ($Section in $SectionTimings.GetEnumerator() | Sort-Object Value -Descending) {
431-
$Percentage = [math]::Round(($Section.Value / $OverallStopwatch.ElapsedMilliseconds) * 100, 2)
432-
Write-Information " $($Section.Key): $($Section.Value)ms ($Percentage%)"
433-
}
434-
Write-Information "========================================`n"
435-
436363
return @($Results)
437364

438365
} catch {

0 commit comments

Comments
 (0)