Revils
Revils

Reputation: 1508

VSTS Release - 'runtime' replacement of Environment Variable

I have made a release in vsts with some environment variables.

One of those environment variables is as follows:

#Array
[ { "name":"password", "value":"thisismypassword" }, { ... } ]

However, I get an output parameter from one of the release tasks which returns the password. So I thought to make a 'tag' and replace it when the output parameter has returned:

[ { "name":"password", "value":"<Password>" } ]

When my output parameter has returned I can create an powershell task to replace the 'tag' with the real password. However to replace, it should be either an string or an valid powershell array. If I directly use the environment variable, it breaks on the first ':' with an error message (because it is not a legit powershell command/format);

#This breaks    
$var = $(environment_variable) 

Hence I thought to convert it to a String, replace it, Convert it back to json object and set it back on the environment variable:

$Setting = ConvertFrom-Json -InputObject '$(environment_variable)'
$Setting = $Setting -replace "<Password>", "$(Output_Password)"

#Tried both below
$Setting_JSON - ConvertTo-Json -InputObject $Setting
$Setting_JNSON = [Newtonsoft.Json.JsonConvert]::SerializeObject($Setting, [Newtonsoft.Json.Formatting]::None)

Write-Host "##vso[task.setvariable variable=$(environment_variable)]$Setting_JSON"

However these produce a json string which is of a different format and the step which uses this variable does not understand;

#Output
["@{name=Password;value=thisisapasswordvalue}"]

#Expected (and needed) Output
[ { "name":"password", "value":"thisisapasswordvalue" } ]

Upvotes: 0

Views: 274

Answers (1)

Eddie Chen - MSFT
Eddie Chen - MSFT

Reputation: 29958

#This breaks    
$var = $(environment_variable) 

For this, you can use this:

$var = $Env:variablename

This works at my side:

$Setting = $env:Var1
$Setting = $Setting -replace "<Password>", "NewValue"

Upvotes: 1

Related Questions