![](/img/trans.png)
[英]How can you use environment variables in an Azure Powershell task in a DevOps Release Pipeline?
[英]Are pipeline variables available in PowerShell task in Azure DevOps?
我有这个 PowerShell 脚本,它读取一些环境变量并输出它们:
Write-Host "name: $(tenantName) version: $(versionRelease) url: $(urlApplication)"
这些变量被定义为管道变量,并且也在 Azure DevOps 发布管道中的变量组中。
在定义为Inline的PowerShell 任务中运行时,此脚本按预期工作。 但是,当 PowerShell 任务配置了File Path时,相同的脚本将不起作用。 在这种情况下, tenantName
env 变量:
tenantName : The term 'tenantName' 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.
有什么方法可以使环境变量对“文件”脚本可用,就像它们对“内联”脚本一样?
我无法回答其中的 Azure 部分,但您的代码对您正在尝试的内容无效。 您需要在$()
块中包含$
。
就像现在一样,Powershell 正在尝试调用名为 tenantName 的tenantName
,或者找到一个名为tenantName.exe
的应用程序来调用。
这将起作用:
Write-Host "name: $($tenantName) version: $($versionRelease) url: $($urlApplication)"
甚至更好:
Write-Host "name: $tenantName version: $versionRelease url: $urlApplication"
如果您需要将一些实际代码放入字符串中,则使用$()
语法,例如引用属性,例如Write-Host "Hello $($user.name)!"
我设法修复它,部分归功于@Cbsch的回答。 实际上,我的 PowerShell 脚本的语法不正确,但是将$(tenantName)
更改为$tenantName
并不能解决问题,因为$tenantName
不存在。 我必须使用$env:<variableName>
语法从环境变量中读取它。
固定脚本:
Write-Host "name: $env:tenantName version: $env:versionRelease url: $env:urlApplication"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.