将 Powershell 脚本中的值返回到 Azure DevOps 任务

kir*_*jan 5 powershell azure azure-devops

我有一个返回字符串值的 Powershell 脚本。我使用 AzurePowerShell 任务调用此脚本,并且希望保留返回的值以便在同一管道内的后续任务中使用它。我怎样才能做到这一点?有什么想法吗?

脚本Powershelltest.ps1

function a {
   <<processing steps>>
   return $(testString)
}
Run Code Online (Sandbox Code Playgroud)

管道:

- task: AzurePowerShell@ 
  ScriptPath: Powershelltest.ps1 (calls the above powershell script)

- task: AzureCLI@
  inlineScript: | 
    use the value from $testString(??)
Run Code Online (Sandbox Code Playgroud)

如何在第一个任务 (AzurePowerShell) 中捕获 Powershell 的返回值并在 AzureCLI 任务中使用它?

谢谢

Nad*_*iss 5

使用此命令:##vso[task.setvariable variable=testString]$testString'在您的第一个任务中。在第二个任务中,您将像这样调用变量:$(testString)。

您的函数应如下所示:

function a {
  <<processing steps>>
  ##vso[task.setvariable variable=testString]$testString'
}
Run Code Online (Sandbox Code Playgroud)

如果只想在后续步骤中使用该值,则不一定需要返回该值。

链接: https: //learn.microsoft.com/en-us/azure/devops/pipelines/process/variables? view=azure-devops&tabs=yaml%2Cbatch#expansion-of-variables