0

I'm trying to update the list object in the following file using Powershell:

{
"$schema":  "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
"contentVersion":  "1.0.0.0",
"parameters":  {
    "environment":  {
            "value":  ""
        },
    "keyVaultAccessPolicies":  {
        "value":  {
            "list": []
        }
    }
}

}

This is my Powershell that I have written so far:

$parametersTemplate = "C:\deploy.parameters.json"
$parametersJson = Get-Content $parametersTemplate -Raw | ConvertFrom-Json
$parametersJson.parameters.keyVaultAccessPolicies.value.list = $armAccessPolicies
$parametersJson | ConvertTo-Json | % { [System.Text.RegularExpressions.Regex]::Unescape($_) } | Set-Content $parametersTemplate

$armAccessPolices is a json string that looks like this:

[
{
    "tenantId":"**************",
    "objectId":"**************",
    "permissions":{"keys":"","secrets":"Get","certificates":"","storage":""}
},
{
    "tenantId":"**************",
    "objectId":"**************",
    "permissions":{"keys":"","secrets":"Get","certificates":"","storage":""}
}
]

When the JSON file is updated the result I am getting is this:

"keyVaultAccessPolicies":  {
      "value":  "@{list=[...]}"
  }

I'd like some advice so the file looks like this:

"keyVaultAccessPolicies":  {
      "value":  {
           "list": [...]
      }
  }
0

1 Answer 1

2

You kind of answer your own question when you say that $armAccessPolices is a json string.

You are appending a string to $parametersJson.parameters.keyVaultAccessPolicies.value.list.

What you want is to append a list. To do so, you need to convert $armAccessPolicies to a PSObject first and then convert everything back to the final json, like this:

$parametersTemplate = "C:\deploy.parameters.json"
$parametersJson = Get-Content $parametersTemplate -Raw | ConvertFrom-Json
# See, we are not setting the json string but rather the resulting psobject.
$parametersJson.parameters.keyVaultAccessPolicies.value.list = ($armAccessPolicies | ConvertFrom-Json) 

$parametersJson | ConvertTo-Json -Depth 10 | % { [System.Text.RegularExpressions.Regex]::Unescape($_) } | Set-Content $parametersTemplate

The only meaningful change here was the .list assignement which now is set to the psobject representation of $armAccessPolicies instead of the raw string through ($armAccessPolicies | ConvertFrom-Json)

Note: You should also define the depth to something bigger than 4 (default) to avoid data loss after the 4th layer.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for this. Works as expected now. Think I’d been staring at it for too long to see the obvious answer.
Also, thanks for the advice on the Depth. I was seeing some objects instead of values and increasing the depths has fixed it.
Is there a way that $armAccessPolicies can always be treated as an array when converted to json? "list" is an array but when $armAccessPolicies.PSobject.BaseObject.Count = 1, "list" is treated as an object so I get a bad json file.
Worked it out: $armAccessPolicies = [PSCustomObject]@{ list = $armAccessPolicies} and then $parametersJson.parameters.keyVaultAccessPolicies.value = $AccessPoliciesObject (update value instead of list)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.