You can use the Set-Variable cmdlet. Passing $global:var3 sends the value of $var3, which is not what you want. You want to send the name.
$global:var1 = $null
function foo (
b, $varName)
{
Set-Variable -Name $varName -Value (
b) -Scope Global
}
foo 1 2 var1
This is not very good programming practice, though. Below would be much more straightforward, and less likely to introduce bugs later:
$global:var1 = $null
function ComputeNewValue (
b)
{
b
}
$global:var1 = ComputeNewValue 1 2
Answer from latkin on Stack OverflowSetting a global PowerShell variable from a function where the global variable name is a variable passed to the function - Stack Overflow
azure devops - How does one set a variable in PowerShell? - Stack Overflow
Permanent Variable Set-Up In Powershell
Set-content how write $variable as it is and not as variable
Videos
You can use the Set-Variable cmdlet. Passing $global:var3 sends the value of $var3, which is not what you want. You want to send the name.
$global:var1 = $null
function foo (
b, $varName)
{
Set-Variable -Name $varName -Value (
b) -Scope Global
}
foo 1 2 var1
This is not very good programming practice, though. Below would be much more straightforward, and less likely to introduce bugs later:
$global:var1 = $null
function ComputeNewValue (
b)
{
b
}
$global:var1 = ComputeNewValue 1 2
As simple as:
$A="1"
function changeA2 () { $global:A="0"}
changeA2
$A
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)
You need to use Write-Host with a special string format:
Write-Host "##vso[task.setvariable variable=YourVSTSVariableName]$yourPowershellVariable"