Created
May 31, 2019 15:46
-
-
Save sdolenc/0da5cda067eb3af5bbc52b0322a84527 to your computer and use it in GitHub Desktop.
Generate local appsettings.json nest structure from keyvault
This file contains 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
# input | |
# a--b--c--d, val | |
# a--b--c--e, val2 | |
# f, val3 | |
# output | |
# { a: { b: { c: { d: val; e: val2 } } }, f: val3 } | |
# this will ask for your azure credentials | |
Connect-AzureRmAccount | |
# this will ask for your keyvault name | |
$keyvault = (Get-AzureKeyVaultSecret).foreach{ Get-AzureKeyVaultSecret -VaultName $_.VaultName -Name $_.Name} | |
$dictionary = @{} | |
foreach ($kv in $keyvault) { | |
$keys = $kv.Name -split "--" | |
$prevDict = $dictionary | |
For ($i=0; $i -le $keys.Count - 2; $i++) { | |
$k = $keys[$i] | |
If (-Not $prevDict.ContainsKey($k)) { | |
$prevDict[$k] = @{} | |
} | |
$prevDict = $prevDict[$k] | |
} | |
$prevDict[$keys[-1]] = $kv.SecretValueText | |
} | |
$dictionary | ConvertTo-Json -depth 99 |
Connect-AzureRmAccount only needs to be run once. I recommend excluding it in future runs within the same terminal
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
simply paste the powershell into your terminal and run or download+invoke the script directly.
It works on powershell 5.1+.
Powershell 6+ will indent the json in the way you'd expect, but you can "lint" or "prettify" it to get the same results