5

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
  • 2
    Please post the code in Plain Text, properly formatted. Commented Oct 16, 2021 at 11:20

1 Answer 1

5

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://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#expansion-of-variables

Sign up to request clarification or add additional context in comments.

2 Comments

I updated my answer with what the code should look like for your PowerShell script. You do not need to return the variable value if you only need it in your other tasks.
Thank you, I have added the line as suggested. My moved a bit forward and hit another blocker. Looks like the powershell script is not called as it comes up with "Module path not present as expected in hosted agent".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.