Search code examples
azureazure-devopsazure-pipelines-release-pipelineazure-cliazure-cli2

Set Output Variable in Azure CLI task on VSTS


I am getting crazy to achieve this very simple task. I need to set an Output Variable in an Azure CLI task on Visual Studio Team Services, because next task in the Release definition will be executed according to the value of this variable.
I wrote this simple code

call az extension add --name azure-cli-iot-ext
call echo ##vso[task.setvariable variable=iotEdgeExists;]$(az iot hub query -n $(iotHub) -q "select * from devices.modules where devices.deviceId ='$(iotEdge)'")

which works, but not as exepected, in fact when I read the Ouput Variable in the next Azure CLI task and I try to print it on the screen I get the command string instead of the output...

call echo"$(az iot hub query -n <IOT_HUB> -q "select * from devices.modules where devices.deviceId ='<IOT_EDGE>'")"

What am I doing wrong?


Solution

  • Refer to this code below:

    call {your command}>tmpFile1
    set /p myvar= < tmpFile1 
    echo "##vso[task.setvariable variable=testvar;]%myvar%"
    

    or

    FOR /F "tokens=* USEBACKQ" %%F IN (`{your command}`) DO (
    SET var=%%F
    )
    echo "##vso[task.setvariable variable=testvar;]%var%"
    

    Mechaflash's answer in How to set commands output as a variable in a batch file