PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, August 19, 2022

[FIXED] How can I set a windows environment variable as part of a pipeline?

 August 19, 2022     azure-pipelines-yaml, environment-variables, powershell     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing