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

在两个PowerShell步骤之间使用Json变量,json,powershell,azure-devops,Json,Powershell,Azure Devops,在Azure Devops管道中,我需要将Json变量从步骤1中的Powershell脚本传递到步骤2中的另一个Powershell脚本。Json变量的双引号似乎把事情搞砸了。逃离它们似乎也不管用 以下是两个步骤: - task: PowerShell@2 displayName: 'Debug -> Step 1' inputs: targetType: 'inline' script: | $json = [pscustomobject]

在Azure Devops管道中,我需要将Json变量从步骤1中的Powershell脚本传递到步骤2中的另一个Powershell脚本。Json变量的双引号似乎把事情搞砸了。逃离它们似乎也不管用

以下是两个步骤:

- 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
这导致:

知道如何将对象(Json格式)传递到另一个步骤吗?

日志记录命令
##vso[task.setvariable]
只能接受一行字符串。您需要使用
-Compress
将json对象转换为单行字符串。请参见以下示例:

$json = [pscustomobject]@{ prop1 = "value1"; prop2 = "value2" } | ConvertTo-Json -Compress
Write-Host "##vso[task.setvariable variable=MYVAR]$json "
日志命令
##vso[task.setvariable]
只能接受一行字符串。您需要使用
-Compress
将json对象转换为单行字符串。请参见以下示例:

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

如果创建JSON的单行表示法可以解决问题,请将
-Compress
添加到
ConvertTo JSON
调用中。很高兴听到这个消息,Bastiaan。因为我不确定我的解决方案是否正确,所以我一开始就发表了评论,但我很高兴有人给出了正确的答案。如果创建JSON的单行表示法解决了问题,请将
-Compress
添加到
转换为JSON
调用中。很高兴听到这个消息,Bastian。因为我不确定我的解决方案是否正确,我一开始就发表了评论,但我很高兴有人给出了正确的答案。