Search code examples
powershellazure-devopsazure-pipelines-release-pipelineazure-artifacts

Vsts pass variable as array of objects


I am trying to pass variable to release pipeline as array of objects. The goal is have in my appsettings.json something like

"myArrayObject": [
      {
        "host": "localhost:2948",
        "protocol": "http"
      }
    ]

In variable tab i created variable called myArrayObject and assigned to it

[{'host':'someUrl','protocol':'https'},{'host':'localhost:44394','protocol':'https'}]

somehow the variable was untouchted by release so then I added this script to pipeline

powershell -command "&{
 $json = Get-Content '.\appsettings.json' -raw | ConvertFrom-Json;
 $json.myArrayObject = '$(myArrayObject)';
 $json | ConvertTo-Json -Depth 32 | Set-Content '.\appsettings.json'; 
 }"

But then I got error

Missing type name after '['.
At line:1 char:129
+ ... llowedUris = [{'host':'someUrl','protoc ...
+                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unexpected token ':'someUrl'' in expression or 
statement.
At line:1 char:169
+ ... lowedUris = [{'host':'someUrl','protoco ...

Is there any way to achieve that ? to pass array of object as variable ?

I also tried this approach:

powershell -command "&{
 $json = Get-Content '.\appsettings.json' -raw | ConvertFrom-Json;
 $json.myArrayObject = '[{"host":"someUrl","protocol":"https"},{"host":"localhost:44394","protocol":"https"}]';
 $json | ConvertTo-Json -Depth 32 | Set-Content '.\appsettings.json'; 
 }"

but at the end i got :

"myArrayObject" : "[{host:someUrl,protocol:https},{host:localhost:44394,protocol:https}]"

Solution

  • this is the solution:

    powershell -command "&{
        $json = Get-Content '.\appsettings.json' -raw | ConvertFrom-Json;
         $data = '$(additionalAllowedUris)' | ConvertFrom-Json;
         $json.additionalAllowedUris= $data.SyncRoot;
         $json | ConvertTo-Json -Depth 32 | Set-Content '.\appsettings.json';
        }"
    
    [{ \"host\" : \"someUrl\",\"protocol\" : \"https\"},{ \"host\":\"localhost:44394\",\"protocol\" : \"https\"},{ \"host\":\"someUrl2\",\"protocol\" : \"https\"}]