2

I am populating a pipeline variable in Powershell task and it is getting populated successfully:

    $env:RESULTJSON=$json
Write-host "##vso[task.setvariable variable=testJSON]$json"

However, when I try to use it to pass the value in a command line task I am not able to do so:

    task: CmdLine@2 
    env:     
      InputJSON: $(testJSON)    
    inputs:      
    script:
        echo %INPUTJSON%        
        'jsonProcessor.exe $(Build.BuildID),%INPUTJSON%'    
    workingDirectory: './jsonProcExe/'

I am getting just "{" in testJSON instead of the whole JSON value.

3
  • Does this help? medium.com/microsoftazure/… Commented Jun 23, 2020 at 11:14
  • What is resultJSON? where is coming from? Commented Jun 23, 2020 at 12:00
  • 1
    Did you look at the documentation? That should answer your question. Commented Jun 23, 2020 at 14:27

1 Answer 1

1

I am not able to get the value of resultJson.

1.If the resultJson is the pipeline variable, you should use InputJSON: $(resultJSON) instead of InputJSON: $($resultJSON).

Assuming the value of pipeline variable resultJSON is 100, then all the tasks within the pipeline can get its value 100. You can modify its value in Task scope using $env:RESULTJSON=$json, then the value of this variable would be the value of $json in that PS task.

But note: Other tasks would get 100 instead of value of $json. (Distinguish the difference between Pipeline Scope and Task Scope)

2.If the resultJson is a variable defined in PS task, then other tasks can't access its value cause the variable is only valid in current session. (You can add second PS task to test it)

To define a variable across multiple tasks/steps, you can use logging command in PS task, check Set variables in scripts .

Update 1

The logging command doesn't support Json format variable directly. So we need to convert it into one line before defining the variable:

$json=$json| ConvertTo-Json -Compress
Write-host "##vso[task.setvariable variable=testJSON]$json"

Update 2

Another direction is to pass the json data via a file. For example first PS task uses $JSON | Out-File Test.json to output the content into Test.json file. And the next PS task uses $Input = Get-Content Test.json and Write-Host $Input to display the value. Also, the second task could be cmd task, their(PS task, CMD task) default working directories are the same one.

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

5 Comments

I have changed the question in accordance to your answer, I am still stuck, can you please help?
@SormitaChakraborty Hmm, so your variable is not normal string variable, instead it's one JSON data variable? Try using ConvertTo-Json -Compress to convert it first before setting variable.
@SormitaChakraborty Azure devops service doesn't support defining and passing json variable directly, you have to convert the json data to pass the variable. Or share the content of json variable via files. See similar issue here. Update with update2.
your first edit worked. json has to be compressed and then passed, thanks a lot.
Glad to know it makes some help :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.