sppc42
sppc42

Reputation: 3272

Azure Pipelines: Setting and accessing variables in Powershell

In Azure pipelines, I have a Powershell task (inline script), where I set a variable

Write-Host "##vso[task.setvariable variable=deployActivity]false"

Immediately afer this line, just for checking purposes that the variable is set, I have the following powershell -

if ($(deployActivity) -eq $true) {
    Write-Host "deployActivity=true (bool)"
}

However, this is failing with the error:

deployActivity : The term 'deployActivity' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Questions -

  1. What am doing above, to retrieve the variable - is it correct/possible? Or is it that a variable can only be refrenced in the follow-up tasks
  2. Why is it failing? Is the referencing of variable in Powershell needs to have any other syntax?

Kindly let me know.

Thanks

Upvotes: 3

Views: 4756

Answers (2)

Shayki Abramczyk
Shayki Abramczyk

Reputation: 41545

Try to wrap the variable with "" - "$(deployActivity)":

if ("$(deployActivity)" -eq $true) {
    Write-Host "deployActivity=true (bool)"
}

Upvotes: 0

Matt
Matt

Reputation: 4035

Per the documentation for that command:

Sets a variable in the variable service of taskcontext. 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.

It will only be available on the tasks that follow setting it. If you need to reference the value in the same script, you should already know what the internal variable should be.

This example YML works for me:

# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml

trigger:
- master

pool:
  vmImage: 'windows-2019'

steps:

    
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: 'Write-Host "##vso[task.setvariable variable=deployActivity]"Example string""'

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: 'Write-Host "Deploy Activity: $(deployActivity)"'

Output of the second task:

Starting: PowerShell
==============================================================================
Task         : PowerShell
Description  : Run a PowerShell script on Linux, macOS, or Windows
Version      : 2.170.1
Author       : Microsoft Corporation
Help         : https://learn.microsoft.com/azure/devops/pipelines/tasks/utility/powershell
==============================================================================
Generating script.
========================== Starting Command Output ===========================
"C:\windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'D:\a\_temp\3a04683f-6159-4bee-862b-2e899e977789.ps1'"
Deploy Activity:  Example string
Finishing: PowerShell

Upvotes: 3

Related Questions