如何在 Powershell 脚本中访问预定义的 Release Management 变量?

How can I access pre-defined Release Management variables in a Powershell script?

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

例如:定义了System.TeamProject变量,应该return当前的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 字段中,但这种做法违背了目的。

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

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