|
| 1 | +function Set-CIPPEnvVarBackup { |
| 2 | + param() |
| 3 | + |
| 4 | + $FunctionAppName = $env:WEBSITE_SITE_NAME |
| 5 | + $PropertiesToBackup = @( |
| 6 | + 'AzureWebJobsStorage' |
| 7 | + 'WEBSITE_RUN_FROM_PACKAGE' |
| 8 | + 'FUNCTIONS_EXTENSION_VERSION' |
| 9 | + 'FUNCTIONS_WORKER_RUNTIME' |
| 10 | + 'CIPP_HOSTED' |
| 11 | + 'CIPP_HOSTED_KV_SUB' |
| 12 | + 'WEBSITE_ENABLE_SYNC_UPDATE_SITE' |
| 13 | + 'WEBSITE_AUTH_AAD_ALLOWED_TENANTS' |
| 14 | + ) |
| 15 | + |
| 16 | + $RequiredProperties = @('AzureWebJobsStorage', 'FUNCTIONS_EXTENSION_VERSION', 'FUNCTIONS_WORKER_RUNTIME', 'WEBSITE_RUN_FROM_PACKAGE') |
| 17 | + |
| 18 | + if ($env:WEBSITE_SKU -eq 'FlexConsumption') { |
| 19 | + $RequiredProperties = $RequiredProperties | Where-Object { $_ -ne 'WEBSITE_RUN_FROM_PACKAGE' } |
| 20 | + } |
| 21 | + |
| 22 | + $Backup = @{} |
| 23 | + foreach ($Property in $PropertiesToBackup) { |
| 24 | + $Backup[$Property] = [environment]::GetEnvironmentVariable($Property) |
| 25 | + } |
| 26 | + |
| 27 | + $EnvBackupTable = Get-CIPPTable -tablename 'EnvVarBackups' |
| 28 | + $CurrentBackup = Get-CIPPAzDataTableEntity @EnvBackupTable -Filter "PartitionKey eq 'EnvVarBackup' and RowKey eq '$FunctionAppName'" |
| 29 | + |
| 30 | + # ConvertFrom-Json returns PSCustomObject - convert to hashtable for consistent key/value access |
| 31 | + $CurrentValues = @{} |
| 32 | + if ($CurrentBackup -and $CurrentBackup.Values) { |
| 33 | + ($CurrentBackup.Values | ConvertFrom-Json).PSObject.Properties | ForEach-Object { |
| 34 | + $CurrentValues[$_.Name] = $_.Value |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + $IsNew = $CurrentValues.Count -eq 0 |
| 39 | + |
| 40 | + if ($IsNew) { |
| 41 | + # First capture - write everything from the live environment |
| 42 | + $SavedValues = $Backup |
| 43 | + Write-Information "Creating new environment variable backup for $FunctionAppName" |
| 44 | + } else { |
| 45 | + # Backup already exists - keep existing values fixed, only backfill any properties not yet captured |
| 46 | + $SavedValues = $CurrentValues |
| 47 | + foreach ($Property in $PropertiesToBackup) { |
| 48 | + if (-not $SavedValues[$Property] -and $Backup[$Property]) { |
| 49 | + Write-Information "Backfilling missing backup property '$Property' from current environment." |
| 50 | + $SavedValues[$Property] = $Backup[$Property] |
| 51 | + } |
| 52 | + } |
| 53 | + Write-Information "Environment variable backup already exists for $FunctionAppName - preserving fixed values" |
| 54 | + } |
| 55 | + |
| 56 | + # Validate all required properties are present in the final backup |
| 57 | + $MissingRequired = $RequiredProperties | Where-Object { -not $SavedValues[$_] } |
| 58 | + if ($MissingRequired) { |
| 59 | + Write-Warning "Environment variable backup for $FunctionAppName is missing required properties: $($MissingRequired -join ', ')" |
| 60 | + } |
| 61 | + |
| 62 | + $Entity = @{ |
| 63 | + PartitionKey = 'EnvVarBackup' |
| 64 | + RowKey = $FunctionAppName |
| 65 | + Values = [string]($SavedValues | ConvertTo-Json -Compress) |
| 66 | + } |
| 67 | + Add-CIPPAzDataTableEntity @EnvBackupTable -Entity $Entity -Force | Out-Null |
| 68 | +} |
0 commit comments