@@ -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 {
0 commit comments