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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Connect-AzureRmAccount only needs to be run once. I recommend excluding it in future runs within the same terminal