Skip to content

Commit d241d67

Browse files
Add bulk support for standards
1 parent 0bfa456 commit d241d67

File tree

2 files changed

+81
-23
lines changed

2 files changed

+81
-23
lines changed

Modules/CIPPCore/Public/Set-CIPPStandardsCompareField.ps1

Lines changed: 71 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,89 @@ function Set-CIPPStandardsCompareField {
33
param (
44
$FieldName,
55
$FieldValue,
6-
$TenantFilter
6+
$TenantFilter,
7+
[Parameter()]
8+
[array]$BulkFields
79
)
810
$Table = Get-CippTable -tablename 'CippStandardsReports'
911
$TenantName = Get-Tenants -TenantFilter $TenantFilter
1012

11-
if ($FieldValue -is [System.Boolean]) {
12-
$FieldValue = [bool]$FieldValue
13-
} elseif ($FieldValue -is [string]) {
14-
$FieldValue = [string]$FieldValue
15-
} else {
16-
$FieldValue = ConvertTo-Json -Compress -InputObject @($FieldValue) -Depth 10 | Out-String
17-
$FieldValue = [string]$FieldValue
13+
# Helper function to normalize field values
14+
function ConvertTo-NormalizedFieldValue {
15+
param($Value)
16+
if ($Value -is [System.Boolean]) {
17+
return [bool]$Value
18+
} elseif ($Value -is [string]) {
19+
return [string]$Value
20+
} else {
21+
$JsonValue = ConvertTo-Json -Compress -InputObject @($Value) -Depth 10 | Out-String
22+
return [string]$JsonValue
23+
}
1824
}
1925

20-
$Existing = Get-CIPPAzDataTableEntity @Table -Filter "PartitionKey eq '$($TenantName.defaultDomainName)' and RowKey eq '$($FieldName)'"
26+
# Handle bulk operations
27+
if ($BulkFields) {
28+
# Get all existing entities for this tenant in one query
29+
$ExistingEntities = Get-CIPPAzDataTableEntity @Table -Filter "PartitionKey eq '$($TenantName.defaultDomainName)'"
30+
$ExistingHash = @{}
31+
foreach ($Entity in $ExistingEntities) {
32+
$ExistingHash[$Entity.RowKey] = $Entity
33+
}
2134

22-
if ($PSCmdlet.ShouldProcess('CIPP Standards Compare', "Set field '$FieldName' to '$FieldValue' for tenant '$($TenantName.defaultDomainName)'")) {
23-
try {
24-
if ($Existing) {
25-
$Existing.Value = $FieldValue
26-
$Existing | Add-Member -NotePropertyName TemplateId -NotePropertyValue ([string]$script:CippStandardInfoStorage.Value.StandardTemplateId) -Force
27-
Add-CIPPAzDataTableEntity @Table -Entity $Existing -Force
35+
# Build array of entities to insert/update
36+
$EntitiesToProcess = [System.Collections.Generic.List[object]]::new()
37+
38+
foreach ($Field in $BulkFields) {
39+
$NormalizedValue = ConvertTo-NormalizedFieldValue -Value $Field.FieldValue
40+
41+
if ($ExistingHash.ContainsKey($Field.FieldName)) {
42+
$Entity = $ExistingHash[$Field.FieldName]
43+
$Entity.Value = $NormalizedValue
44+
$Entity | Add-Member -NotePropertyName TemplateId -NotePropertyValue ([string]$script:CippStandardInfoStorage.Value.StandardTemplateId) -Force
2845
} else {
29-
$Result = [PSCustomObject]@{
46+
$Entity = [PSCustomObject]@{
3047
PartitionKey = [string]$TenantName.defaultDomainName
31-
RowKey = [string]$FieldName
32-
Value = $FieldValue
48+
RowKey = [string]$Field.FieldName
49+
Value = $NormalizedValue
3350
TemplateId = [string]$script:CippStandardInfoStorage.Value.StandardTemplateId
3451
}
35-
Add-CIPPAzDataTableEntity @Table -Entity $Result -Force
3652
}
37-
Write-Information "Adding $FieldName to StandardCompare for $Tenant. content is $FieldValue"
38-
} catch {
39-
Write-Warning "Failed to add $FieldName to StandardCompare for $Tenant. content is $FieldValue - $($_.Exception.Message)"
53+
$EntitiesToProcess.Add($Entity)
54+
}
55+
56+
if ($PSCmdlet.ShouldProcess('CIPP Standards Compare', "Set $($EntitiesToProcess.Count) fields for tenant '$($TenantName.defaultDomainName)'")) {
57+
try {
58+
# Single bulk insert/update operation
59+
Add-CIPPAzDataTableEntity @Table -Entity $EntitiesToProcess -Force
60+
Write-Information "Bulk added $($EntitiesToProcess.Count) fields to StandardCompare for $($TenantName.defaultDomainName)"
61+
} catch {
62+
Write-Warning "Failed to bulk add fields to StandardCompare for $($TenantName.defaultDomainName) - $($_.Exception.Message)"
63+
}
64+
}
65+
} else {
66+
# Original single field logic
67+
$NormalizedValue = ConvertTo-NormalizedFieldValue -Value $FieldValue
68+
$Existing = Get-CIPPAzDataTableEntity @Table -Filter "PartitionKey eq '$($TenantName.defaultDomainName)' and RowKey eq '$($FieldName)'"
69+
70+
if ($PSCmdlet.ShouldProcess('CIPP Standards Compare', "Set field '$FieldName' to '$NormalizedValue' for tenant '$($TenantName.defaultDomainName)'")) {
71+
try {
72+
if ($Existing) {
73+
$Existing.Value = $NormalizedValue
74+
$Existing | Add-Member -NotePropertyName TemplateId -NotePropertyValue ([string]$script:CippStandardInfoStorage.Value.StandardTemplateId) -Force
75+
Add-CIPPAzDataTableEntity @Table -Entity $Existing -Force
76+
} else {
77+
$Result = [PSCustomObject]@{
78+
PartitionKey = [string]$TenantName.defaultDomainName
79+
RowKey = [string]$FieldName
80+
Value = $NormalizedValue
81+
TemplateId = [string]$script:CippStandardInfoStorage.Value.StandardTemplateId
82+
}
83+
Add-CIPPAzDataTableEntity @Table -Entity $Result -Force
84+
}
85+
Write-Information "Adding $FieldName to StandardCompare for $($TenantName.defaultDomainName). content is $NormalizedValue"
86+
} catch {
87+
Write-Warning "Failed to add $FieldName to StandardCompare for $($TenantName.defaultDomainName). content is $NormalizedValue - $($_.Exception.Message)"
88+
}
4089
}
4190
}
4291
}

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,12 +407,21 @@ function Get-CIPPStandards {
407407
$TestResult = Test-CIPPStandardLicense -StandardName 'IntuneTemplate_general' -TenantFilter $TenantName -RequiredCapabilities @('INTUNE_A', 'MDM_Services', 'EMS', 'SCCM', 'MICROSOFTINTUNEPLAN1')
408408
if (-not $TestResult) {
409409
$IntuneKeys = @($ComputedStandards.Keys | Where-Object { $_ -like '*IntuneTemplate*' })
410+
# Collect all fields for bulk insert
411+
$BulkFields = [System.Collections.Generic.List[object]]::new()
410412
foreach ($Key in $IntuneKeys) {
411413
$TemplateKey = ($Key -split '\|', 2)[1]
412414
if ($TemplateKey) {
413-
Set-CIPPStandardsCompareField -FieldName "standards.IntuneTemplate.$TemplateKey" -FieldValue 'This tenant does not have the required license for this standard.' -Tenant $TenantName
415+
$BulkFields.Add([PSCustomObject]@{
416+
FieldName = "standards.IntuneTemplate.$TemplateKey"
417+
FieldValue = 'This tenant does not have the required license for this standard.'
418+
})
414419
}
415420
}
421+
# Single bulk insert operation
422+
if ($BulkFields.Count -gt 0) {
423+
Set-CIPPStandardsCompareField -TenantFilter $TenantName -BulkFields $BulkFields
424+
}
416425
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."
417426
foreach ($Key in $IntuneKeys) { [void]$ComputedStandards.Remove($Key) }
418427
} else {

0 commit comments

Comments
 (0)