Skip to content

Commit

Permalink
Merge pull request #1096 from JohnDuprey/dev
Browse files Browse the repository at this point in the history
CIPPBackup fix
  • Loading branch information
JohnDuprey authored Aug 28, 2024
2 parents a033f08 + 4f94919 commit e1ecbd2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
23 changes: 14 additions & 9 deletions Modules/CIPPCore/Public/Add-CIPPAzDataTableEntity.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,34 @@ function Add-CIPPAzDataTableEntity {
} catch [System.Exception] {
if ($_.Exception.ErrorCode -eq 'PropertyValueTooLarge' -or $_.Exception.ErrorCode -eq 'EntityTooLarge' -or $_.Exception.ErrorCode -eq 'RequestBodyTooLarge') {
try {
$largePropertyNames = [System.Collections.ArrayList]::new()
$largePropertyNames = [System.Collections.Generic.List[string]]::new()
$entitySize = 0

# Convert $SingleEnt to hashtable if it is a PSObject
if ($SingleEnt -is [System.Management.Automation.PSCustomObject]) {
$SingleEnt = $SingleEnt | ConvertTo-Json -Depth 100 | ConvertFrom-Json -AsHashtable
}

foreach ($key in $SingleEnt.Keys) {
$propertySize = [System.Text.Encoding]::UTF8.GetByteCount($SingleEnt[$key].ToString())
$entitySize = $entitySize + $propertySize
if ($propertySize -gt $MaxSize) {
$largePropertyNames.Add($key)
}

}

if ($largePropertyNames.Count -gt 0) {
$splitInfoList = [System.Collections.ArrayList]@()
$splitInfoList = [System.Collections.Generic.List[object]]::new()
foreach ($largePropertyName in $largePropertyNames) {
$dataString = $SingleEnt[$largePropertyName]
$splitCount = [math]::Ceiling($dataString.Length / $MaxSize)
$splitData = [System.Collections.ArrayList]@()
$splitData = [System.Collections.Generic.List[object]]::new()
for ($i = 0; $i -lt $splitCount; $i++) {
$start = $i * $MaxSize
$splitData.Add($dataString.Substring($start, [Math]::Min($MaxSize, $dataString.Length - $start))) > $null
}

$splitPropertyNames = [System.Collections.ArrayList]@()
$splitPropertyNames = [System.Collections.Generic.List[object]]::new()
for ($i = 0; $i -lt $splitData.Count; $i++) {
$splitPropertyNames.Add("${largePropertyName}_Part$i") > $null
}
Expand All @@ -61,7 +66,7 @@ function Add-CIPPAzDataTableEntity {
# Check if the entity is still too large
$entitySize = [System.Text.Encoding]::UTF8.GetByteCount($($SingleEnt | ConvertTo-Json))
if ($entitySize -gt $MaxRowSize) {
$rows = [System.Collections.ArrayList]@()
$rows = [System.Collections.Generic.List[object]]::new()
$originalPartitionKey = $SingleEnt.PartitionKey
$originalRowKey = $SingleEnt.RowKey
$entityIndex = 0
Expand All @@ -79,21 +84,21 @@ function Add-CIPPAzDataTableEntity {
$newEntity['PartIndex'] = $entityIndex
$entityIndex++

$propertiesToRemove = [System.Collections.ArrayList]@()
$propertiesToRemove = [System.Collections.Generic.List[object]]::new()
foreach ($key in $SingleEnt.Keys) {
$newEntitySize = [System.Text.Encoding]::UTF8.GetByteCount($($newEntity | ConvertTo-Json))
if ($newEntitySize -lt $MaxRowSize) {
$propertySize = [System.Text.Encoding]::UTF8.GetByteCount($SingleEnt[$key].ToString())
if ($propertySize -gt $MaxRowSize) {
$dataString = $SingleEnt[$key]
$splitCount = [math]::Ceiling($dataString.Length / $MaxSize)
$splitData = [System.Collections.ArrayList]@()
$splitData = [System.Collections.Generic.List[object]]::new()
for ($i = 0; $i -lt $splitCount; $i++) {
$start = $i * $MaxSize
$splitData.Add($dataString.Substring($start, [Math]::Min($MaxSize, $dataString.Length - $start))) > $null
}

$splitPropertyNames = [System.Collections.ArrayList]@()
$splitPropertyNames = [System.Collections.Generic.List[object]]::new()
for ($i = 0; $i -lt $splitData.Count; $i++) {
$splitPropertyNames.Add("${key}_Part$i") > $null
}
Expand Down
14 changes: 6 additions & 8 deletions Modules/CIPPCore/Public/New-CIPPBackup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ function New-CIPPBackup {
'CIPP' {
try {
$BackupTables = @(
'bpa'
'Config'
'Domains'
'ExcludedLicenses'
Expand All @@ -25,20 +24,19 @@ function New-CIPPBackup {
)
$CSVfile = foreach ($CSVTable in $BackupTables) {
$Table = Get-CippTable -tablename $CSVTable
Get-AzDataTableEntity @Table | Select-Object *, @{l = 'table'; e = { $CSVTable } }
Get-AzDataTableEntity @Table | Select-Object *, @{l = 'table'; e = { $CSVTable } } -ExcludeProperty DomainAnalyser
}
Write-LogMessage -user $request.headers.'x-ms-client-principal' -API $APINAME -message 'Created backup' -Sev 'Debug'
$CSVfile
$RowKey = 'CIPPBackup' + '_' + (Get-Date).ToString('yyyy-MM-dd-HHmm')
$entity = [PSCustomObject]@{
$CSVFile = [string]($CSVfile | ConvertTo-Json -Compress -Depth 100)
$entity = @{
PartitionKey = 'CIPPBackup'
RowKey = $RowKey
RowKey = [string]$RowKey
TenantFilter = 'CIPPBackup'
Backup = [string]($CSVfile | ConvertTo-Json -Compress -Depth 100)
Backup = $CSVfile
}
$Table = Get-CippTable -tablename 'CIPPBackup'
try {
$Result = Add-AzDataTableEntity @Table -entity $entity -Force
$Result = Add-CIPPAzDataTableEntity @Table -Entity $entity -Force
Write-LogMessage -user $request.headers.'x-ms-client-principal' -API $APINAME -message 'Created CIPP Backup' -Sev 'Debug'
} catch {
$ErrorMessage = Get-CippException -Exception $_
Expand Down

0 comments on commit e1ecbd2

Please sign in to comment.