powershellazure-pipelinesazure-devops-server-2020

""Your build pipeline references an undefined variable" : it's just a powershell variable


I have a Powershell task in a job in a Pipeline in Azure Devops Server 2020. Here's part of the 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
           }
       }

This works fine in the task -- my builds are doing what I want. But when I view the YAML I see comments like this:

#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

Again, this doesn't interfere with the execution of the script.

However, now I want to extract this Task into a Task Group. When I do so, the harmless misdetection is now a problem, because it insists these are new parameters:

screenshot of Task Group dialog

Is there some magic I can do to change my Powershell script so they are not thought to be parameters?


Solution

  • Your build pipeline references an undefined variable named ‘$add.key’

    This is triggered by the line below:

    Write-Output "Found matching environment variable for key: $($add.key)"
    

    The $($add.key) is parsed by Azure DevOps as macro syntax. You could avoid this by using string formatting:

    'Found matching environment variable for key: {0}' -f $add.key
    

    Btw, in most cases you don't need to use Write-Output - it's slow and superfluous. See this blog post for details: Let’s Kill Write-Output.