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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Sormita Chakraborty
  • 1,015
  • 2
  • 19
  • 36

1 Answers1

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
LoLance
  • 25,666
  • 1
  • 39
  • 73
  • I have changed the question in accordance to your answer, I am still stuck, can you please help? – Sormita Chakraborty Jun 24 '20 at 05:08
  • 1
    @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. – LoLance Jun 24 '20 at 06:00
  • @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](https://stackoverflow.com/questions/54460959/how-to-use-json-variables-in-an-azure-devops-pipeline). Update with update2. – LoLance Jun 24 '20 at 06:19
  • your first edit worked. json has to be compressed and then passed, thanks a lot. – Sormita Chakraborty Jun 24 '20 at 06:28
  • Glad to know it makes some help :) – LoLance Jun 24 '20 at 06:30