Return values from Powershell script into Azure devops tasks

Viewed 837

I have a Powershell script which returns a string value. I'm invoking this script using a AzurePowerShell task and I want to retain the returned value to use it in my subsequent tasks within the same pipeline. How can I do that? Any ideas, please?

Script Powershelltest.ps1:

function a {
   <<processing steps>>
   return $(testString)
}

Pipeline:

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

- task: AzureCLI@
  inlineScript: | 
    use the value from $testString(??)

How can I catch the return value from Powershell in the first task (AzurePowerShell) and use it in AzureCLI task?

Thanks

1 Answers

Use this command: ##vso[task.setvariable variable=testString]$testString' in your first task. In your second task you will call the variable like this: $(testString).

Your function should look like the following:

function a {
  <<processing steps>>
  ##vso[task.setvariable variable=testString]$testString'
}

You don't necessarily need to return the value if only want to use it in a following step.

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

Related