Sree Nair
Sree Nair

Reputation: 101

Pass variable value from one Powershell script in AZ Powershell task to another script in the next AZ Powershell task in Azure DevOps Release Pipeline

I have an Azure DevOps release pipeline that I am experimenting with. The first task in the release pipeline is an AZ Powershell task. This task has a Powershell Script file. The below is the content of the file -

[CmdletBinding()]
param (
   $MyVar
)
$MyVar = "My Value"
Write-Host '##vso[task.setvariable variable=NewVar;]$MyVar'

The second task in the pipeline again is an AZ Powershell task with a Powershell script with the below content -

[CmdletBinding()]
param (
   $MyNewVar
)

$MyNewVar = "$(NewVar)"
Write-Host $MyNewVar

I run the simple release pipeline and I receive the below error

Cannot validate argument on parameter 'NewVar'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.

Please note that the same works fine when running the same Powershell code Inline in each of the AZ Powershell tasks.

Any help would be highly appreciated.

Upvotes: 0

Views: 1121

Answers (2)

Sree Nair
Sree Nair

Reputation: 101

I added a new argument as below in the second task -

-$MyNewVar "$(NewVar)"

This as well didn't work successfully but then in the first task, I changed the last line containing single quotes to double, i.e.

Write-Host '##vso[task.setvariable variable=NewVar;]$MyVar' 

changes to

Write-Host "##vso[task.setvariable variable=NewVar;]$MyVar"

This finally resolved the issue.

Upvotes: 1

Shayki Abramczyk
Shayki Abramczyk

Reputation: 41595

If you don't use inline script you can't use the syntax $MyNewVar = "$(NewVar)", the $() syntax it's for Azure DevOps tasks, PS not recognize it.

You should use the environment variable syntax:

$MyNewVar = "$env:NewVar"

Upvotes: 0

Related Questions