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