Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I retrieve variables in a PowerShell script running as part of a VSTS build/release?

I am getting an error in VSTS when I run a powershell script.

I have powershell install on my agent. But I still get this error:

The term 'Get-VstsInput' is not recognized as the name of a cmdlet, function, script file, or operable program.

I am I missing something.

like image 808
gopal Avatar asked Jan 03 '23 01:01

gopal


2 Answers

You can access variables in a PowerShell script with $env:VariableName. The only exception is secret variables. Those have to be explicitly passed into the script via a param block.

For example, if you had a variable named $(Foo), you could access it in a PowerShell script as $env:Foo. If there are periods in the variable name, they are replaced with underscores. So $(Foo.Bar) becomes $env:Foo_Bar.

This also does not apply to PowerShell on Target Machines. If you run a PowerShell script on a remote machine via that task, you have to pass any variables as arguments, as the build agent's environment variables are not populated on the target machine(s).

There is no need to use the VSTS SDK unless you are writing a custom task.

like image 52
Daniel Mann Avatar answered Jan 13 '23 15:01

Daniel Mann


Get-VstsInput is used for developing build/release tasks (not used for executing script in PowerShell task). More details, you can refer VSTS DevOps Task SDK and vsts-task-lib commands.

  • If you want to use a defined variable in PowerShell task, you can use it with the format $(variableName).
  • If you want to get all the accessible variables for the build/release by PowerShell task, you can use the command ls env:.
like image 35
Marina Liu Avatar answered Jan 13 '23 14:01

Marina Liu