Azure DevOps-在PowerShell脚本中设置和使用变量 [英] Azure DevOps - Setting and Using Variables in PowerShell Scripts

查看:247
本文介绍了Azure DevOps-在PowerShell脚本中设置和使用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Azure DevOps构建管道,该管道具有两个单独的PowerShell脚本.在第一个脚本中,我从XML文件中获取一个值,并在环境变量中设置该值.在第二个脚本中,我想使用环境变量中的值.不幸的是,我看不到环境变量被设置.目前,我有:

I have an Azure DevOps build pipeline that has two separate PowerShell scripts. In the first script, I am getting a value from an XML file and setting that value in an environment variable. In my second script, I want to use the value in the environment variable. Unfortunately, I don't see the environment variable getting set. At this time, I have:

脚本1:

$myXml = [xml](Get-Content ./MyXml.xml)  
$departmentId = $myXml.Department.Id

Write-Host ##vso[task.setvariable variable=DepartmentId;]$departmentId    
Write-Host "Set environment variable to ($env:DepartmentId)"

Get-ChildItem Env:

Write-Host "Department Id ($departmentId)"

运行脚本1时,我看到:

When script 1 runs, I see:

Set environment variable to ()
[All of the environment variable BUT, I DO NOT SEE ONE NAMED "DepartmentId"]
Department Id (1)

注意:1)$env:DepartmentId值未在设置环境变量"语句中打印,并且2)DepartmentId值未在环境变量列表中列出.我的意图是在第二个脚本中使用DepartmentId,如下所示:

Notice: 1) The $env:DepartmentId value is not printing in the "Set environment variable" statement and 2) The DepartmentId value is NOT listed in the environment variable list. My intention is to use DepartmentId in the second script, which looks like this:

脚本2:

Write-Host "Using Department: $(env:DepartmentId)"

这时,脚本仅显示:

env:DepartmentId : The term 'env:DepartmentId' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

我看过其他相关的SO问题,并查看了文档.但是,这根本行不通.我不明白我在做什么错.有人可以告诉我如何解决此问题并解释我做错了什么吗?谢谢!

I've seen the other related SO questions and reviewed the docs. However, this simply isn't working. I don't understand what I'm doing wrong. Can someone please show me how to fix this and explain what I'm doing wrong? Thank you!

推荐答案

脚本1

通过task.setvariable设置环境变量时请使用引号,因为#表示PowerShell注释.您已注释掉要输出的字符串.

Use quotes when setting the environment variable via task.setvariable since # signifies a PowerShell comment. You have commented out the string you intend to output.

还请注意,由于管道必须首先在输出中处理task.setvariable,因此环境变量在设置脚本的脚本中可能不可用.

Also note that the environment variable may not be available in the script where you set it since the pipeline must first process task.setvariable in the output.

$myXml = [xml](Get-Content ./MyXml.xml)  
$departmentId = $myXml.Department.Id

Write-Host "##vso[task.setvariable variable=DepartmentId;]$departmentId"
Write-Host "Set environment variable to ($env:DepartmentId)"

Get-ChildItem Env:

Write-Host "Department Id ($departmentId)"

脚本2

您仍然必须通过表达式内的$引用变量.您在env之前缺少$.

You must still reference variables via $ inside an expression. You're missing $ before env.

Write-Host "Using Department: $($env:DepartmentId)"

这篇关于Azure DevOps-在PowerShell脚本中设置和使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆