我在 Azure Devops Server 2020 的管道作业中有一个 Powershell 任务。以下是 Powershell 的一部分:
$xml = [xml](Get-Content $configPath)
Write-Output "Iterating over appSettings"
ForEach($add in $xml.configuration.appSettings.add)
{
#Write-Output "Processing AppSetting key $($add.key)"
$SecretVarKey = "MAPPED_"+$add.key
Write-Output $SecretVarKey
$matchingEnvVar = [Environment]::GetEnvironmentVariable($SecretVarKey)
if($matchingEnvVar)
{
Write-Output "Found matching environment variable for key: $($add.key)"
Write-Output "Replacing value $($add.value) with $matchingEnvVar"
$add.value = $matchingEnvVar
}
}
这在任务中工作得很好——我的构建正在做我想要的事情。但是当我查看 YAML 时,我看到这样的注释:
#Your build pipeline references an undefined variable named ‘$add.key’. Create or edit the build pipeline for this YAML file, define the variable on the Variables tab. See https://go.microsoft.com/fwlink/?linkid=865972
同样,这不会干扰脚本的执行。
但是,现在我想将此任务提取到任务组中。当我这样做时,无害的误检测现在成为一个问题,因为它坚持认为这些是新参数:
我可以使用一些魔法来更改我的 Powershell 脚本,这样它们就不会被认为是参数吗?
最佳答案
Your build pipeline references an undefined variable named ‘$add.key’
这是由下面的行触发的:
Write-Output "Found matching environment variable for key: $($add.key)"
$($add.key)
由 Azure DevOps 解析为 macro syntax 。您可以通过使用字符串格式来避免这种情况:
'Found matching environment variable for key: {0}' -f $add.key
顺便说一句,在大多数情况下,您不需要使用 Write-Output - 它很慢而且是多余的。有关详细信息,请参阅此博文:Let’s Kill Write-Output .
关于powershell - ""您的构建管道引用了 undefined variable ": it' 只是一个 powershell 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68511256/