如何从 Powershell 脚本文件访问 Azure DevOps 预定义变量
How to access Azure DevOps Predefined variables from Powershell script file
我有一个 Powershell 脚本,我在 ADO 发布阶段通过 Powershell 任务文件路径方法 运行(ps1 文件在我的 wwwroot/scripts 文件夹中)。
在 Powershell 脚本中,我需要访问 Build.BuildId 来做一些工作,但是,它在那个变量上爆炸了,其中之前我通过“内联”方法 运行 这段代码, 效果很好。
我不能 运行 脚本“内联”,因为我们让这个脚本做很多事情并且它超过了 Powershell 任务内联脚本字符限制。
如何从文件中访问 Build.BuildId 变量?
Build.BuildId : The term 'Build.BuildId' is not recognized as the name of a cmdlet, function, script file, or operable
2022-02-16T18:58:30.4256566Z program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
2022-02-16T18:58:30.4257898Z At D:\_agent1\_work\r11\a\...\wwwroot\scripts\myscript.ps1:70 char:90
2022-02-16T18:58:30.4259534Z + ...$project/_apis/build/builds/$(Build.BuildId)/workit ...
名称为upper-cased,.
替换为_
- PowerShell 脚本: $env:VARIABLE_NAME $env:BUILD_BUILDID
Powershell script Parameters 又名参数
在您的 powershell 脚本顶部定义一个参数:
param(
[string] $buildId
)
Write-Host "The build id is $buildId";
并在管道中,添加一个参数以在 powershell task
中提交该参数的值
- task: PowerShell@2
inputs:
targetType: filePath
filePath: /wwwroot/scripts/myscript.ps1
arguments: -buildId $(Build.BuildId)
为此,参数比环境变量更好,原因有很多:
- 脚本明确说明了它需要什么输入
- 管道明确说明它正在提供什么输入
- 您可以轻松地 运行 本地脚本(例如用于测试),为每个参数提供合适的值
- 你可以添加parameter attributes,例如你可以明确地使一个参数成为必需的,或者给它一个默认值
我有一个 Powershell 脚本,我在 ADO 发布阶段通过 Powershell 任务文件路径方法 运行(ps1 文件在我的 wwwroot/scripts 文件夹中)。
在 Powershell 脚本中,我需要访问 Build.BuildId 来做一些工作,但是,它在那个变量上爆炸了,其中之前我通过“内联”方法 运行 这段代码, 效果很好。
我不能 运行 脚本“内联”,因为我们让这个脚本做很多事情并且它超过了 Powershell 任务内联脚本字符限制。
如何从文件中访问 Build.BuildId 变量?
Build.BuildId : The term 'Build.BuildId' is not recognized as the name of a cmdlet, function, script file, or operable
2022-02-16T18:58:30.4256566Z program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
2022-02-16T18:58:30.4257898Z At D:\_agent1\_work\r11\a\...\wwwroot\scripts\myscript.ps1:70 char:90
2022-02-16T18:58:30.4259534Z + ...$project/_apis/build/builds/$(Build.BuildId)/workit ...
名称为upper-cased,.
替换为_
- PowerShell 脚本: $env:VARIABLE_NAME $env:BUILD_BUILDID
Powershell script Parameters 又名参数
在您的 powershell 脚本顶部定义一个参数:
param(
[string] $buildId
)
Write-Host "The build id is $buildId";
并在管道中,添加一个参数以在 powershell task
中提交该参数的值- task: PowerShell@2
inputs:
targetType: filePath
filePath: /wwwroot/scripts/myscript.ps1
arguments: -buildId $(Build.BuildId)
为此,参数比环境变量更好,原因有很多:
- 脚本明确说明了它需要什么输入
- 管道明确说明它正在提供什么输入
- 您可以轻松地 运行 本地脚本(例如用于测试),为每个参数提供合适的值
- 你可以添加parameter attributes,例如你可以明确地使一个参数成为必需的,或者给它一个默认值