powershell - ""您的构建管道引用了 undefined variable ": it' 只是一个 powershell 变量

标签 powershell azure-pipelines azure-devops-server-2020

我在 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

同样,这不会干扰脚本的执行。

但是,现在我想将此任务提取到任务组中。当我这样做时,无害的误检测现在成为一个问题,因为它坚持认为这些是新参数:

screenshot of Task Group dialog

我可以使用一些魔法来更改我的 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/

相关文章:

Azure devOps 2020 RTW - 无法登录 Azure CLI - 使用 PAT

git - 如何在 Azure DevOPS 服务器上使用 PAT 克隆 git 存储库

powershell - 如何在powershell函数中获取当前正在执行的文件

docker - Azure管道: downloading docker image multiple times

git - 如何编辑/更新构建任务 TFS 中的默认获取源步骤?

angular - NX Monorepo 的 Azure 静态 Web 应用程序部署管道错误

powershell - 如何防止Powershell中出现此无限循环?

shell - 进行键绑定(bind)以运行上一个或最后一个 shell 命令

windows - Chef::Exceptions::WindowsNotAdmin:由于缺少管理员权限,无法获取安全信息