簡體   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. Escaping 它們似乎也不起作用。

這里有兩個步驟:

- 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

這導致:

在此處輸入圖像描述

知道如何將 object (在 Json 中)傳遞到另一個步驟嗎?

日志記錄命令##vso[task.setvariable]只能接受單行字符串。 您需要使用-Compress將 json object 轉換為單行字符串。 請參見下面的示例:

$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