我有一個 Powershell 腳本,我通過 Powershell 任務檔案路徑方法在 ADO 發布階段運行(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 ...
uj5u.com熱心網友回復:
名稱是大寫的,并且.替換為_
- PowerShell 腳本: $env:VARIABLE_NAME $env:BUILD_BUILDID
參考:https ://docs.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml,batch#environment-variables
uj5u.com熱心網友回復:
Powershell腳本引數又名引數
在 powershell 腳本的頂部定義一個引數:
param(
[string] $buildId
)
Write-Host "The build id is $buildId";
并在管道中,添加一個引數以在您的powershell 任務中提交該引數的值
- task: PowerShell@2
inputs:
targetType: filePath
filePath: /wwwroot/scripts/myscript.ps1
arguments: -buildId $(Build.BuildId)
出于多種原因,引數比環境變數更好:
- 該腳本明確說明了它需要什么輸入
- 管道明確說明了它提供的輸入
- 您可以輕松地在本地運行腳本(例如用于測驗),為每個引數提供合適的值
- 您可以添加引數屬性,例如,您可以顯式設定強制引數,或者給它一個默認值
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/426222.html
標籤:天蓝色 电源外壳 天蓝色的devops
下一篇:KQL中的自定義日期格式
