简体   繁体   English

在两个 PowerShell 步骤之间使用 Json 变量

[英]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. 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. Escaping 它们似乎也不起作用。

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?知道如何将 object (在 Json 中)传递到另一个步骤吗?

The logging command ##vso[task.setvariable] can only accept a single line string.日志记录命令##vso[task.setvariable]只能接受单行字符串。 You need to use -Compress to convert the json object to a single line string.您需要使用-Compress将 json object 转换为单行字符串。 See below example:请参见下面的示例:

$json = [pscustomobject]@{ prop1 = "value1"; prop2 = "value2" } | ConvertTo-Json -Compress
Write-Host "##vso[task.setvariable variable=MYVAR]$json "

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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