Skip to content

Instantly share code, notes, and snippets.

@sdolenc
Created May 31, 2019 15:46
Show Gist options
  • Save sdolenc/0da5cda067eb3af5bbc52b0322a84527 to your computer and use it in GitHub Desktop.
Save sdolenc/0da5cda067eb3af5bbc52b0322a84527 to your computer and use it in GitHub Desktop.
Generate local appsettings.json nest structure from keyvault
# 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
@sdolenc
Copy link
Author

sdolenc commented May 31, 2019

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

@sdolenc
Copy link
Author

sdolenc commented May 31, 2019

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