0
votes

I have a powershell script, which is not in a job, task, or stage, it's on its own. running in my DevOps build yaml like this:

- powershell: |
   if (//something that's irrelevant) {
    Write-Host "##vso[task.setvariable variable=myCustomVar;isOutput=true]true"
   } else {
    Write-Host "##vso[task.setvariable variable=myCustomVar;isOutput=true]false"
   }

After this powershell script, I have another script to echo out myCustomVar to see what the value is. Like this:

- script: |
    echo "What is my custom variable?"
    echo $(myCustomVar)

When the build runs, in the devops logs, it echos literally "$(myCustomVar)" and not either True/False

After that, I have a task which sends an email, but we only want to send an email if myCustomVar is true. So I use a conditional.

- task: SendEmail@1
  displayName: "Send email"
  condition: and(succeeded(), eq(variables.myCustomVar, true))

however, this is breaking. I've tried a few other ways of doing it. myCustomVar, on the task condition, always returns NULL. Any help on syntax?

enter image description here

1
Set-Variable only affects the local scope of the running PowerShell task - you need to output a special macro string to set a pipeline variable from inside the script ā€“ Mathias R. Jessen
I read about this but this only seems to work with jobs and stages, which Iā€™m not implementing. ā€“ J.G.Sable

1 Answers

0
votes

You could use ##vso[task.setvariable]value to set variables from a script in pipeline. The first task can set a variable, and following tasks are able to use the variable. The variable is exposed to the following tasks as an environment variable.

Check following useful links: