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...
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)...