3

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

1

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

5 Comments

I altered my tasks - removed the if expression and put that in a subsequent Powerhshell script, but I am still getting the same error in this new script now.
is it an inline script or a file script?
its an inline script
@Matt - that didn't work either - Powershell takes that as a Powershell variable which is undefined
@Matt - This got it wokring. Infact, I added another task after resetting the value in second task and the value indeed gets updated in the next task only, not in the same task in which it was set. Thanks!
0

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

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

1 Comment

Can you share your full pipeline and the full log?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.