I've got a yaml pipeline that needs to set (create if not there) a windows environment variable on a VM that is to be used by another program on the VM. How do I go about doing that?
I've tried looking at this question (amongst other articles) but none of the answers worked. They either needed the .NET SDK installed or didn't fail but didn't create the variable. I get the impression this should be possible using powershell, so very similar to one of the answers in that question I tried this:
- task: PowerShell@2
displayName: Set environment variables.
inputs:
targetType: 'inline'
script: |
Write-Host "##vso[task.setvariable variable=MY_KEY;]$(TestSecret)"
Like I say, this doesn't fail, but when I check the VM environment variables 'MY_KEY' doesn't exist.
UPDATE
Following the suggestion below from Lee_Dailey I tried the following:
- task: PowerShell@2
displayName: Set environment variables.
inputs:
targetType: 'inline'
script: |
[System.Environment]::SetEnvironmentVariable("MY_KEY", "$($env:TestSecret)", "Machine")
env:
TestSecret: $(TestSecret)
I also tried replacing the actual value of the key with just "testKey" rather than using a variable, as well as setting [Environment] rather than [System.Environment]. In all cases the pipeline ran without errors but no environment / system variable was created on the VM with the name MY_KEY.
I double checked it was running this on the right machine by writing the computer name to the console, so I know it's not somehow doing this on another machine.