Tao*_*Gil 7 powershell environment-variables azure-devops
我有这个 PowerShell 脚本,它读取一些环境变量并输出它们:
Write-Host "name: $(tenantName) version: $(versionRelease) url: $(urlApplication)"
Run Code Online (Sandbox Code Playgroud)
这些变量定义为管道变量,并且也定义在 Azure DevOps 发布管道的变量组中。
当在定义为Inline 的PowerShell 任务中运行时,此脚本按预期工作。但是,当使用File Path配置 PowerShell 任务时,相同的脚本将无法工作。在这种情况下,找不到 env 变量:tenantName
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.
Run Code Online (Sandbox Code Playgroud)
有没有办法使环境变量可用于“文件”脚本,就像“内联”脚本一样?
我设法修复了它,部分归功于@Cbsch 的回答。事实上,我的 PowerShell 脚本的语法不正确,但更改$(tenantName)为 并$tenantName没有解决问题,因为它$tenantName不存在。我必须使用$env:<variableName>语法从环境变量中读取它。
固定脚本:
Write-Host "name: $env:tenantName version: $env:versionRelease url: $env:urlApplication"
Run Code Online (Sandbox Code Playgroud)