@@ -23,31 +23,32 @@ function Add-CIPPAzDataTableEntity {
2323 $Parameters.OperationType = $OperationType
2424 }
2525
26- $MaxRowSize = 500000 - 100 # Maximum size of an entity
27- $MaxSize = 30 kb # Maximum size of a property value
26+ $MaxRowSize = 500000 - 100
27+ $MaxSize = 30 kb
2828
2929 foreach ($SingleEnt in @ ($Entity )) {
3030 try {
3131 if ($null -eq $SingleEnt.PartitionKey -or $null -eq $SingleEnt.RowKey ) {
3232 throw ' PartitionKey or RowKey is null'
3333 }
34+
3435 Add-AzDataTableEntity @Parameters - Entity $SingleEnt - ErrorAction Stop
36+
3537 } catch [System.Exception ] {
36- if ($_.Exception.ErrorCode -eq ' PropertyValueTooLarge' -or $_ .Exception.ErrorCode -eq ' EntityTooLarge' -or $_ .Exception.ErrorCode -eq ' RequestBodyTooLarge' ) {
38+ if ($_.Exception.ErrorCode -in @ ( ' PropertyValueTooLarge' , ' EntityTooLarge' , ' RequestBodyTooLarge' ) ) {
3739 try {
3840 Write-Host ' Entity is too large. Splitting entity into multiple parts.'
39- # Write-Information ($SingleEnt | ConvertTo-Json)
41+
4042 $largePropertyNames = [System.Collections.Generic.List [string ]]::new()
4143 $entitySize = 0
4244
43- # Convert $SingleEnt to hashtable if it is a PSObject
4445 if ($SingleEnt -is [System.Management.Automation.PSCustomObject ]) {
4546 $SingleEnt = $SingleEnt | ConvertTo-Json - Depth 100 - Compress | ConvertFrom-Json - AsHashtable
4647 }
4748
4849 foreach ($key in $SingleEnt.Keys ) {
4950 $propertySize = [System.Text.Encoding ]::UTF8.GetByteCount($SingleEnt [$key ].ToString())
50- $entitySize = $entitySize + $propertySize
51+ $entitySize += $propertySize
5152 if ($propertySize -gt $MaxSize ) {
5253 $largePropertyNames.Add ($key )
5354 }
@@ -63,7 +64,7 @@ function Add-CIPPAzDataTableEntity {
6364 $start = $i * $MaxSize
6465 $splitData.Add ($dataString.Substring ($start , [Math ]::Min($MaxSize , $dataString.Length - $start ))) > $null
6566 }
66- $splitDataCount = ( $splitData | Measure-Object ) .Count
67+ $splitDataCount = $splitData.Count
6768 $splitPropertyNames = [System.Collections.Generic.List [object ]]::new()
6869 for ($i = 0 ; $i -lt $splitDataCount ; $i ++ ) {
6970 $splitPropertyNames.Add (" ${largePropertyName} _Part$i " )
@@ -80,11 +81,9 @@ function Add-CIPPAzDataTableEntity {
8081 $SingleEnt [$splitPropertyNames [$i ]] = $splitData [$i ]
8182 }
8283 }
83-
8484 $SingleEnt [' SplitOverProps' ] = ($splitInfoList | ConvertTo-Json - Compress).ToString()
8585 }
8686
87- # Check if the entity is still too large
8887 $entitySize = [System.Text.Encoding ]::UTF8.GetByteCount($ ($SingleEnt | ConvertTo-Json - Compress))
8988 if ($entitySize -gt $MaxRowSize ) {
9089 $rows = [System.Collections.Generic.List [object ]]::new()
@@ -96,11 +95,7 @@ function Add-CIPPAzDataTableEntity {
9695 Write-Information " Entity size is $entitySize . Splitting entity into multiple parts."
9796 $newEntity = @ {}
9897 $newEntity [' PartitionKey' ] = $originalPartitionKey
99- if ($entityIndex -eq 0 ) {
100- $newEntity [' RowKey' ] = $originalRowKey
101- } else {
102- $newEntity [' RowKey' ] = " $ ( $originalRowKey ) -part$entityIndex "
103- }
98+ $newEntity [' RowKey' ] = if ($entityIndex -eq 0 ) { $originalRowKey } else { " $ ( $originalRowKey ) -part$entityIndex " }
10499 $newEntity [' OriginalEntityId' ] = $originalRowKey
105100 $newEntity [' PartIndex' ] = $entityIndex
106101 $entityIndex ++
@@ -142,12 +137,11 @@ function Add-CIPPAzDataTableEntity {
142137 $entitySize = [System.Text.Encoding ]::UTF8.GetByteCount($ ($SingleEnt | ConvertTo-Json - Compress))
143138 }
144139
145- if (( $SingleEnt | Measure-Object ) .Count -gt 0 ) {
140+ if ($SingleEnt.Count -gt 0 ) {
146141 $SingleEnt [' RowKey' ] = " $ ( $originalRowKey ) -part$entityIndex "
147142 $SingleEnt [' OriginalEntityId' ] = $originalRowKey
148143 $SingleEnt [' PartIndex' ] = $entityIndex
149144 $SingleEnt [' PartitionKey' ] = $originalPartitionKey
150-
151145 $rows.Add ($SingleEnt )
152146 }
153147
@@ -156,14 +150,28 @@ function Add-CIPPAzDataTableEntity {
156150 $NewRow = ([PSCustomObject ]$row ) | Select-Object * - ExcludeProperty Timestamp
157151 Add-AzDataTableEntity @Parameters - Entity $NewRow
158152 }
153+
159154 } else {
160155 $NewEnt = ([PSCustomObject ]$SingleEnt ) | Select-Object * - ExcludeProperty Timestamp
161156 Add-AzDataTableEntity @Parameters - Entity $NewEnt
157+ if ($NewEnt.PSObject.Properties [' OriginalEntityId' ] -eq $null -and $NewEnt.PSObject.Properties [' PartIndex' ] -eq $null ) {
158+ $partIndex = 1
159+ while ($true ) {
160+ $partRowKey = " $ ( $NewEnt.RowKey ) -part$partIndex "
161+ try {
162+ Remove-AzDataTableEntity - Context $Context - PartitionKey $NewEnt.PartitionKey - RowKey $partRowKey - ErrorAction Stop
163+ Write-Information " Deleted obsolete part: $partRowKey "
164+ $partIndex ++
165+ } catch {
166+ break
167+ }
168+ }
169+ }
162170 }
163171
164172 } catch {
165173 $ErrorMessage = Get-NormalizedError - Message $_.Exception.Message
166- Write-Warning ( ' AzBobbyTables Error' )
174+ Write-Warning ' AzBobbyTables Error'
167175 Write-Information ($SingleEnt | ConvertTo-Json )
168176 throw " Error processing entity: $ErrorMessage Linenumber: $ ( $_.InvocationInfo.ScriptLineNumber ) "
169177 }
0 commit comments