HiveBrain v1.2.0
Get Started
← Back to all entries
patternshellMinor

Azure Devops Pipeline variables in Powershell

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
pipelinepowershellazurevariablesdevops

Problem

I've tried to figure this out and I can't see why my ipaddress is not getting set.

I've checked code samples and they all suggest I am doing everything correct.

ip variable defined here:

This is my powershell script:

# Write your PowerShell commands here.

Write-Host "AGENTIPADDRESS variable is set to ($env:AGENTIPADDRESS)"

$ipaddress = Invoke-RestMethod http://ipinfo.io/json | Select -exp ip
Write-Host "ipaddress variable is set to ($ipaddress)"
Write-Host "ip pipeline variable to ($ip)"

Write-Host "##vso[task.setvariable variable=IP;]$ipaddress"
Write-Host "##vso[task.setvariable variable=env:IP;]$ipaddress"
Write-Host "##vso[task.setvariable variable=$env:IP;]$ipaddress"
echo "##vso[task.setvariable variable=IP;]$ipaddress"
Write-Host "##vso[task.setvariable variable=IP;]333"
Write-Host "##vso[task.setvariable variable=IP;]$ipaddress"
echo "##vso[task.setvariable variable=IP]999"
echo "##vso[task.setvariable variable=IP;]888"

Write-Host "##vso[task.setvariable variable=IP;]$ipaddress"
Write-Host "##vso[task.setvariable variable=IP;]$ipaddress"
Write-Host "##vso[task.setvariable variable=IP]114"
Write-Host "##vso[task.setvariable variable=IP;]123"

Write-Host "ip environment variable to ($ip)"
Write-Host "env:IP environment variable to ($env:IP)"

Write-Host "Set environment variable to ($env:AGENTIPADDRESS)"


And here is my output:

Why can't I set & read the ip variable?

Solution

Pipeline Variables are handed to PowerShell as environment variables, which means that $ip exists only with the script while you want to use $env:ip to pick the environment variable value.

Note Secret variables are not passed automatically: you must add an explicit env: in YAML or as arguments in Classic Editor. The latter means that the PowerShell code must have a param declaration (e.g. param($ip))and the Task use Arguments field (e.g. -ip $(ip)).

Context

StackExchange DevOps Q#13616, answer score: 3

Revisions (0)

No revisions yet.