0
votes

I am following this link to set a variable in a task such that it can be used in another task in the same agent job.

In a PowerShell script task I am using the foll. line to set variable:

Write-Host "##vso[task.setvariable variable=sauce]crushed tomatoes"
  1. The next section on the link shows how to read variable - it has got arguments and script. Where do I have to enter the arguments? Is it into another powershell task? This next part suggests that I also need to set isOutput=true when the variable needs to be used by another task - https://docs.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=classic%2Cbatch#using-variables-as-task-inputs

  2. I am confused between accessing the variable set in above step. If I want to fetch the value in another PowerShell task or if I want to pass the value to another non-powershell task, then should I use $(sauce) or $env:SAUCE to fetch/send the value?

1

1 Answers

0
votes

Yes, you can read the variable with another PowerShell task, the simple way is:

  • For inline script:

    Write-Host $(sauce)

  • For file script:

    Write-Host $env:sauce

You don't need to add the isOutput=true.

My YAML:

steps:
- task: PowerShell@2
  displayName: 'Set var'
  inputs:
    targetType: filePath
    filePath: ./test.ps1

- powershell: |
   Write-Host $(sauce)
 displayName: 'Read inline'

- task: PowerShell@2
  displayName: 'Read from file'
  inputs:
    targetType: filePath
    filePath: ./read.ps1

The file test.ps1 is:

enter image description here

The file read.pd1 is:

enter image description here

The build output is:

enter image description here