Azure DevOPS PowerShell task output and input parameters - powershell

I have a Task Group with a PowerShell task. This task returns variables as outputs.
In my pipeline I have two tasks after this Task Group task:
One that is an ARM deployment task: I'm able to retrieve the content of the output variables from the PowerShell (with the "$(variablename)" syntax)
One that is another Task Group with a PowerShell task in it. When I pass the "$(variableName)" token as an input parameter for this task, it is not interpreted. In the PowerShell script I received the string "$(variableName)", and not the value of the variable itself...
Am I missing something?

Using ##vso syntax to set the output variable in the first task group.
Write-Host "##vso[task.setvariable variable=var1;isOutput=true;]test"
In the subsequent tasks or task group, get the variable in the form of task name + ordinal combination. e.g. $(Powershell1.VariableName)
You can refer to this ticket with similar issue .

Related

How to read release pipeline variable and use as environment variable in Azure DevOps in Azure CLI task?

I am using one powershell task and one Azure Cli task in release pipeline of Azure DevOps.
I have some release pipeline's variables. I want to read those variables as they will be required by my script in above two tasks. I used powershell core in Azure cli task
I tried to read them in the inline script directly as $(variableName) or $env:variableName but none of the above worked.
I tried to set read the variable in Environment variables option in the task and then use in the inline scripts using $env:variableName but it also didn't work. On printing the variableName in the script using Write-Host, the value I got is $(valueName) instead of the correct value.
How to read those variables inside these scripts?
The pipeline variables can be reference in the Azure CLI inline script by using the syntax $(variableName). I tested by adding the following in a script
write-host "The variable value: $(variableName)"

How to set Azure DevOps pipeline variable with Powershell

I currently have a variable in my release pipeline and need to set its value through a Powershell script. The purpose is to have its value available to be used for postman collections in next tasks.
I'm trying to do that in this way but not working.
$content = Get-Content -Path .\token.txt
Write-Host "RP token found: $content"
Write-Host "##vso[task.setvariable variable=readingProgressToken;]$content"
Write-Host "Variable value in pipeline: $(readingProgressToken)"
And this is the variable
variable
Using the set variable command will make the variable available for all the task steps that follow. It will not be available within the scope of the same task. If you break your task into two steps, one set then one test display, I'd expect you would see the setting is probably going to be as-expected for your postman step.
From the documentation:
To set a variable from a script, use the task.setvariable logging
command. This doesn't update the environment variables, but it does
make the new variable available to downstream steps within the same
job.
In the script task (PowerShell, Bash, etc..), if you use the logging command SetVariable to create or update a variable with the specified value, this variable with the new value is only available to the subsequent tasks in the same job.
For the script task which runs the SetVariable command, the new value is not available.
So, if you want to print the new value of the variable, you should print it in another task after the script task.

Set an output variable from an agentless job

I have a pipeline that runs an agentless job. This job produces a result that I would like to pass to the next or next job. The problem is that all the examples I've found set variables on agent jobs, not agentless. See here, all the examples use script commands, which need to be run on an agent.
Is there a way to set an output variable from an agentless job? How else can I pass the result from an agentless job to the next?
Setting output variables from agentless jobs isn't supported
powershell runs Windows PowerShell and will only work on a Windows agent.
https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/utility/powershell?view=azure-devops
Depending on what your use-case is, you may be able to use dependsOn and condition in your jobs to achieve your goal. E.g. for retries for builds that are not idempotent. Otherwise an agent-based configuration may be needed.

Azure Devops Pipeline: Define variable and use it as task input on another step

I have Azure DevOps release pipeline job having 4 tasks inside it.
I would like to set the environment variable in the first task and to use that value as input to the second task for parameter: Display name.
We can assume all 4 steps are PowerShell scripts.
Task 1 Powershell:
Write-Host "##vso[task.setvariable variable=myvariable;]abcdefg"
Task 2 Powershell:
PowerShell inside task 2:
Write-Host "$(myvariable)"
How could I set a variable in task 1 and access it as an input variable to task2?
My output was:
Task2 - $(myvariable) as display name
but PowerShell script itself output was:
abcdefg
Write-Host "##vso[task.setvariable variable=myvariable;]abcdefg"
That's because the variable this script created is not a pre-defined variable.
As the execution logic, after you queue the pipeline, you can see that the pipeline name, task name are displayed firstly, even though the script does not be executed. So, if these names are using variable to define them, the variable just can get the value from the pre-defined variable. Because the compile of variable value get in pipeline/task name are always firstly than the script executed.
In addition, the script in task just create a script variable, and this script variable only live for the lifetime of the Phase and are destroyed after execution.
How could I set a variable in task 1 and access it as an input
variable to task2?
As you what want, if get it in another script, just use $(xxx) or $env:xxx. But, for name, $() just can get the pre-defined variable value, instead of the script variable value.

Read a user input while queue a VSTS Build

I want to pass few arguments to the VSTS build tasks while queue a build (The same way we do for Jenkins). How can i do that.
I want to read a comma seperated string and want to pass that argument to a Windows Batch Script task in VSTS build job.
I am new to VSTS, someone please help.
Refer to these steps below:
Add a variable in build definition
Check Settable at queue time option
Specify that variable in Batch Script task
On the Variables tab, add a variable and check the "Settable at queue time" box. The variable will then appear in the queue build dialog, and will appear as an environment variable in your script.