MikeJ
MikeJ

Reputation: 87

How do you set a variables in powershell

I am trying to set variables in a PowerShell so I can use them in custom conditions in a release definitions, so i can prevent phases from running is a VSTS variable is set to false or 0

Upvotes: 3

Views: 18274

Answers (4)

Ciaran O'Neill
Ciaran O'Neill

Reputation: 2634

You need to use Write-Host with a special string format:

Write-Host "##vso[task.setvariable variable=YourVSTSVariableName]$yourPowershellVariable"

Upvotes: 7

Marina Liu
Marina Liu

Reputation: 38136

Even you can set variable value, but you can not use the modified variable value to prevent phases from running, since variables can not persist between agent phases.

And you can also refer the post Passing release variables between two agent phases.

And for the way to set variable in build, you can refer the document Define and modify your variables in a script.

Upvotes: 0

HerbM
HerbM

Reputation: 571

Setting variables in PowerShell itself is trivial, merely assign (= operator) to the variable:

$VarName = 42

But likely the question is looking for a way to set Environment variables which the (VSTS) application can 'see' and 'use' when it is called from PowerShell.

To assign to any session Environment variable prefix the name with "ENV" so it looks like this:

$Env:VarName = 4201

This will remain for the current PowerShell session or life of the console -- other sessions in other windows or run later will not see or be affected by such settings.

If you wish to have a persistent Environment variable then you must set that in the registry, either for the User (HKey_Local_User) or Computer (Hkey_Local_Machine).

These will get the environment settings from the registry (maybe be different that currently set in your process):

[Environment]::GetEnvironmentVariable('path',[EnvironmentVariableTarget]::Machine)
[Environment]::GetEnvironmentVariable('path',[EnvironmentVariableTarget]::User)

And the Set commands are similar but include the new value (e.g., variable 'Tools' is set to 'C:\':

[Environment]::SetEnvironmentVariable('Tools', 'C:\', [EnvironmentVariableTarget]::Machine)
[Environment]::SetEnvironmentVariable('Tools', 'C:\', [EnvironmentVariableTarget]::User)

There is also a "location" enumeration for "Process" (instead of User or Machine) but it is simpler to set this using the PowerShell $Env:VariableName shown above.

    [Environment]::SetEnvironmentVariable('Tools', 'C:\', [EnvironmentVariableTarget]::Process)

Upvotes: 6

Adam
Adam

Reputation: 4178

I apologize. I'm not 100% sure I understand what you're trying to do.

Scenario-1: You want to set some variables outside of VSTS that are used by a Release Definition Inside VSTS

(a) I'd set those as environment variables on the host. You can call them from scripts that run on that host. (b) Set those variables in a separate Powershell script, then dot-source that script from a Powershell-task-script.

Scenario-2: You want to pass variable values from VSTS to a Powershell script

You're going to the Build and Release tab, selecting Releases, and then selecting the action New Definition. You're creating an empty process. You add a new artifact to the process. You eventually get to add a Powershell task (one of the utility tasks). For that task, you can select the script that gets run and the arguments you pass into it. Those arguments are parameters in the script. This like step 8/9 in the Create a release definition of the CI/CD Hello world article.

Hope this helps!

Upvotes: 0

Related Questions