简体   繁体   中英

Use Json variable between two PowerShell steps

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:

在此处输入图像描述

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

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 "

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM