如何在Powershell脚本中访问预定义的发布管理变量?

时间:2016-06-07 15:35:33

标签: powershell tfs2015 ms-release-management azure-pipelines

根据documentation,有许多预定义变量可用于在发布管理环境中运行的任务。如何从Powershell脚本中访问这些变量?

例如:System.TeamProject变量已定义,应返回当前的TFS TeamProject。但是,当我写:

Write-Host "environment var: $env:System.TeamProjectId"

日志文件中的输出是:

2016-06-07T09:26:49.5537161Z environment var: release.TeamProject

但是,在Initialize日志文件中,将显示以下内容:

4 2016-06-07T09:26:40.4121001Z Environment variables available are below.  Note that these environment variables can be referred to in the task (in the ReleaseDefinition) by replacing "_" with "." e.g. AGENT_NAME environment variable can be referenced using Agent.Name in the ReleaseDefinition: 
 ...
34 2016-06-07T09:26:40.4277002Z [SYSTEM_COLLECTIONID] --> [2043d9ba-7ec9-43f0-8e6c-96a8b28f55d8]
35 2016-06-07T09:26:40.4277002Z [SYSTEM_TEAMPROJECTID] --> [9718773d-2aee-4625-91c6-80de16301479]
36 2016-06-07T09:26:40.4277002Z [SYSTEM_TEAMPROJECT] --> [MyProject]
37 2016-06-07T09:26:40.4277002Z [SYSTEM_CULTURE] --> [en-US]

所以这意味着变量就在那里。我按照其他地方的建议尝试$(System.TeamProject),但是The term ... is not recognized失败了 - 错误。

此外,我自己在发布定义中配置的变量,例如priority,我 能够使用$env:priority访问

作为一种解决方法,我可以在脚本中创建自己的参数,并在任务定义的Arguments字段中传递它们,但这种方法会失败。

1 个答案:

答案 0 :(得分:3)

使用花括号,因为变量名称包含.。例如:

PS C:\> ${Env:System.TeamProject} = "Var contents"
PS C:\> ${Env:System.TeamProject}
Var contents
PS C:\> Write-Host "Prefix - $Env:System.TeamProject"
Prefix - .TeamProject
PS C:\> Write-Host "Prefix - ${Env:System.TeamProject}"
Prefix - Var contents