Last active
June 27, 2024 04:46
-
-
Save thushan/e8ae6565b5607d831a478c1ac18418f2 to your computer and use it in GitHub Desktop.
Azure App Settings to local.settings.json converter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
Azure App Settings / Environment Variables to local.settings.json Converter | |
| v1.0.1 |----------------------------------------------------------------- | |
This script will take an Azure App Settings JSON file and convert it to a | |
local.settings.json file for use with Azure Functions. | |
It also moves some of the Azure specific settings to the top of the file. | |
Usage: | |
.\Convert-AzureAppSettingsToLocalSettings.ps1 "appsettings.json" "local.settings.json" | |
Example: | |
appsettings.json: | |
[ | |
{ | |
"name": "XkcdApiKey", | |
"value": "cd39809326cc", | |
"slotSetting": false | |
}, | |
{ | |
"name": "FUNCTIONS_WORKER_RUNTIME", | |
"value": "dotnet-isolated", | |
"slotSetting": false | |
}, | |
{ | |
"name": "FUNCTIONS_EXTENSION_VERSION", | |
"value": "~4", | |
"slotSetting": false | |
}, | |
{ | |
"name": "APPINSIGHTS_INSTRUMENTATIONKEY", | |
"value": "ABCDSFDKJSDKGLSJDKLGJSDG", | |
"slotSetting": false | |
} | |
] | |
local.settings.json: | |
{ | |
"IsEncrypted": false, | |
"Values": { | |
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated", | |
"FUNCTIONS_EXTENSION_VERSION": "~4", | |
"APPINSIGHTS_INSTRUMENTATIONKEY": "ABCDSFDKJSDKGLSJDKLGJSDG", | |
"XkcdApiKey": "cd39809326cc", | |
} | |
} | |
#> | |
$azureAppSettingsFilePath = $args[0] | |
$outputLocalSettingsFPath = if ($args[1]) { $args[1] } else { "local.settings.json" } | |
# We don't need these from Azures in our local settings | |
$ignoredKeys = @( | |
"AZURE_FUNCTIONS_ENVIRONMENT", | |
"WEBSITE_RUN_FROM_PACKAGE" | |
) | |
# Make the script neater by having these up the top. | |
$headerKeys = @( | |
"FUNCTIONS_WORKER_RUNTIME", "FUNCTIONS_EXTENSION_VERSION", | |
"APPINSIGHTS_INSTRUMENTATIONKEY", "AzureWebJobsStorage", | |
"WEBSITE_CONTENTAZUREFILECONNECTIONSTRING", "WEBSITE_CONTENTSHARE" | |
) | |
$filterOutKeys = $ignoredKeys + $headerKeys | |
$azureAppSettings = Get-Content -Path $azureAppSettingsFilePath | ConvertFrom-Json | |
$sortedAzureAppSettings = $azureAppSettings.GetEnumerator() | Sort-Object Name | |
$localSettings = [ordered]@{} | |
$headerSettings = [ordered]@{} | |
foreach ($setting in $sortedAzureAppSettings) { | |
if ($setting.Name -notin $filterOutKeys) { | |
$localSettings[$setting.Name] = $setting.Value | |
} elseif ($setting.Name -in $headerKeys) { | |
$headerSettings[$setting.Name] = $setting.Value | |
} | |
} | |
$localSettingsJson = @{ | |
"IsEncrypted" = $false | |
"Values" = $headerSettings + $localSettings | |
} | |
$localSettingsJsonString = $localSettingsJson | ConvertTo-Json -Depth 100 | |
$localSettingsJsonString | Out-File -FilePath $outputLocalSettingsFPath |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment