0

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:

enter image description 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:

enter image description here

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

1

The issue is that you can't reference the changed value in the same task.

I created a new Powershell task and just called Write-Host "env:IP environment variable set to ($env:IP)" which output the IP address correctly.

0

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)).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.