Issue
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.
Solution
Turns out one of my earlier attempts actually did work. However, the way I was checking the variables wasn't picking up on it (command prompt using 'set' to read them all, didn't restart the session). So I checked through the system settings GUI and found it was getting set after all. I ended up with this:
- task: PowerShell@2
displayName: Set environment variables.
inputs:
targetType: 'inline'
script: |
[System.Environment]::SetEnvironmentVariable("MY_KEY", "$(TestSecret)", [System.EnvironmentVariableTarget]::Machine)
Answered By - sr28 Answer Checked By - David Marino (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.