0

In an Azure Devops Pipeline I need to pass over a Json variable from a Powershell script in step 1 to another Powershell script in step 2. The double quotes of the Json variable seem to be messing things up. Escaping them also does not seem to work.

Here the 2 steps:

- task: PowerShell@2
  displayName: 'Debug -> Step 1'
  inputs:
    targetType: 'inline'
    script: |      
      $json = [pscustomobject]@{ prop1 = "value1"; prop2 = "value2" } | ConvertTo-Json
      Write-Host "##vso[task.setvariable variable=MYVAR]$json"

- task: PowerShell@2
  displayName: 'Debug -> Step 2'
  inputs:
    targetType: 'inline'
    script: |
      echo $env:MYVAR

This results in:

enter image description here

Any idea how I can pass an object (in Json) to another step?

2
  • 3
    If creating a single-line representation of your JSON solves the problem, add -Compress to the ConvertTo-Json call. Commented Jun 23, 2020 at 13:43
  • 1
    Glad to hear it, Bastiaan. Because I wasn't sure if my solution was the right one, I started out with a comment, but I'm glad someone has posted a proper answer since. Commented Jun 24, 2020 at 10:47

1 Answer 1

3

The logging command ##vso[task.setvariable] can only accept a single line string. You need to use -Compress to convert the json object to a single line string. See below example:

$json = [pscustomobject]@{ prop1 = "value1"; prop2 = "value2" } | ConvertTo-Json -Compress
Write-Host "##vso[task.setvariable variable=MYVAR]$json "
Sign up to request clarification or add additional context in comments.

Comments

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.