0

I'm trying to set a Powershell variable in an Azure devops pipeline, then retrieve that variable in a subsequent task. But the variable cannot be found.

In the first Powershell task I've set the inline script to...

Install-Module -Name SqlServer -Force

$result = Invoke-Sqlcmd `
   -ConnectionString "Server=..." `
   -Query "SELECT TOP (1) work_item FROM ..."

Write-Host $result.work_item # This does write the expected value.

$lastWorkItem = $result.work_item
Write-Host $lastWorkItem # This does write the expected value.

Write-Host "##vso[task.setvariable variable=lastWorkItem;isOutput=true]$lastWorkItem"

And in a subsequent Powershell task I have simply...

Write-Host $(lastWorkItem)

Here is the result...

enter image description here

I've also tried...

  • removing the isOutput=true
  • retrieving it using $(env:lastWorkItem)
  • using Powershell Core and 'legacy' Powershell

No difference.

What am I doing wrong? I've looked (very closely) at this SO thread and can't see any difference.

UPDATE
I'm using the classic pipelines, not YAML pipelines.

And if I try to store the variable without ## (as per comment below)...

enter image description here

awj
  • 7,482
  • 10
  • 66
  • 120

1 Answers1

1

This example does not work. Check how to use output variables: Use output variables from tasks

steps:
- powershell: |
   $lastWorkItem = "1111"
   
   Write-Host "##vso[task.setvariable variable=lastWorkItem;isOutput=true]$lastWorkItem"
  displayName: 'PowerShell Script'

- powershell: 'Write-Host $(lastWorkItem)'
  displayName: 'PowerShell Script'

enter image description here

However, this should work. Set variables in scripts

steps:
- powershell: |
   $lastWorkItem = "1111"
   
   Write-Host "##vso[task.setvariable variable=lastWorkItem]$lastWorkItem"
  displayName: 'PowerShell Script'

- powershell: 'Write-Host $(lastWorkItem)'
  displayName: 'PowerShell Script'

enter image description here

Shamrai Aleksander
  • 13,096
  • 3
  • 24
  • 31
  • Sorry, I should have said that I'm using a classic pipeline, not a YAML pipeline. This shouldn't make a difference to the Powershell command, however, and the command that you have tested and proven is exactly the same command that I have in the OP (although I have `isReadOnly=true`, though I've also explained that removing this makes no difference in my case.) So what's different about your working version and my task? – awj Mar 24 '23 at 09:20
  • @awj I've tested that on the classic pipeline. yaml just for a simple view. The issues may be in your `$lastWorkItem`... is it some object or just a string? can you share the result of `Write-Host $lastWorkItem` in the first step? – Shamrai Aleksander Mar 24 '23 at 09:27
  • As you can see in the OP, `$lastWorkItem` is the output of `Invoke-SqlCommand` - `$result` is the direct output from that command, then I obtain the desired value using `$result.work_item`. In the DB, that column is an `int` field. – awj Mar 24 '23 at 10:38
  • @awj test what you see on the set variable task. Use write-host without `##` like: `Write-Host "vso[task.setvariable variable=lastWorkItem]$lastWorkItem"` – Shamrai Aleksander Mar 24 '23 at 10:44
  • Ok, updated the OP with a second screenshot. – awj Mar 24 '23 at 11:00
  • @awj why do you still use `isOutput=true`? It will not work at the same build stage.... – Shamrai Aleksander Mar 24 '23 at 11:03
  • Ok, I've removed the `isOuput=true` but it makes no difference. – awj Mar 24 '23 at 11:11