我有一個 azure devops 管道,我想將保存 json 的變數的內容寫入文本檔案。
以下是管道中的兩個任務:
- task: CmdLine@2
displayName: 'echo swagger content'
inputs:
script: |
echo "print value of swaggerContent output variable set in get-swagger-from-azure.ps1"
echo $(swaggerContent)
- task: PowerShell@2
displayName: 'write swagger content to file'
inputs:
targetType: 'inline'
script: $env:swaggerContent | Out-File "$(Pipeline.Workspace)/swagger-content.json"'
CmdLine 任務作業正常并輸出 json,如下所示:

但是,PowerShell 任務給出以下錯誤:
在 D:\a_temp\05c70744-c4cc-4322-99a0-98f55e41fbba.ps1:7 char:1
- } 別的 {
- ~ 運算式或陳述句中出現意外的標記 '}'。
- CategoryInfo : ParserError: (:) [], ParseException
- FullyQualifiedErrorId : UnexpectedToken
有人看到我做錯了什么嗎?
uj5u.com熱心網友回復:
$(swaggerContent)是 Azure Pipelines 變數,而不是 PowerShell 變數。它只是一個包含 JSON 的占位符。
所以在行
$(swaggerContent) | ConvertTo-Json | Out-File "$(Pipeline.Workspace)/swagger-content.json"
想想如果你只是$(swaggerContent)用一些 JSON 替換會發生什么。你得到類似的東西
{ "foo": "bar" } | ConvertTo-Json | Out-File "$(Pipeline.Workspace)/swagger-content.json"
請注意,JSON 是完全未轉義的。它不是一個字串,它只是插入腳本中間的隨機文本。
Azure Pipelines 在運行腳本時將非機密變數視為環境變數,因此您可以嘗試以下方式:
$env:swaggerContent | Out-File "$(Pipeline.Workspace)/swagger-content.json"
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/527283.html
下一篇:如何在遠程會話中設定區域變數?
