如何将Windows环境变量设置为管道的一部分?

发布于 2025-01-30 06:42:07 字数 1137 浏览 4 评论 0原文

我有一个YAML管道,需要在VM上使用另一个程序使用的VM上的Windows环境变量(如果没有)。我该怎么做?

我已经尝试查看这个问题(在其他文章中),但所有答案都没有起作用。他们要么需要安装.NET SDK,要么没有失败,但没有创建变量。我得到的印象应该使用PowerShell,这与我尝试过的一个答案之一非常相似:

- task: PowerShell@2
      displayName: Set environment variables.
      inputs:
        targetType: 'inline'
        script: |
          Write-Host "##vso[task.setvariable variable=MY_KEY;]$(TestSecret)"

就像我说的那样,这并没有失败,但是当我检查VM环境变量时,My_key'''存在。

更新

遵循以下建议,我尝试了以下内容:

- task: PowerShell@2
      displayName: Set environment variables.
      inputs:
        targetType: 'inline'
        script: | 
          [System.Environment]::SetEnvironmentVariable("MY_KEY", "$($env:TestSecret)", "Machine")
      env:
        TestSecret: $(TestSecret)

我还尝试用仅“ TestKey”而不是使用变量以及设置[环境]而不是[系统。环境]。在所有情况下,管道都没有错误,但是在VM上没有创建带有MY_KEY的环境 /系统变量。

我通过将计算机名称写在控制台上,仔细检查了它在右机器上运行的,所以我知道它不是在另一台计算机上以某种方式这样做。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

莫多说 2025-02-06 06:42:07

事实证明,我较早的尝试实际上确实有效。但是,我检查变量的方式并没有在其上拾取(命令提示符使用“设置”读取所有内容,没有重新启动会话)。因此,我检查了系统设置GUI,发现它毕竟已经设置了。我最终得到了:

- task: PowerShell@2
      displayName: Set environment variables.
      inputs:
        targetType: 'inline'
        script: | 
          [System.Environment]::SetEnvironmentVariable("MY_KEY", "$(TestSecret)", [System.EnvironmentVariableTarget]::Machine)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文