title: Azure Pipeline Loops
body: <p>I've spent quite a bit of time on this now and can't seem to get it right so need to ask for advise.</p>
  <p>I've got a GitHub repo which contains a series of ZIP files. For each Zip file I want to execute a template in DevOps, there could be 1 there could be 10. For each file I want to perform the same checks.</p>
  <p>I wrote a PowerShell task that scans a gathers the list of zip files. I spent considerable time attemping to use OutPut variables and variables but it seems that you can't leverage an array at all using those in Azure Pipelines.</p>
  <pre><code>    steps:
    - ${{ each var in variables }}:
      - task: PowerShell@2
        displayName: 'Checking variables ${{ var.key }}'
        inputs:
          targetType: Inline
          script: |
            Write-Host ${{ var.name }}
            Write-Host ${{ var.key }}
            Write-Host ${{ var.value }}
            Write-Host &quot;${{ convertToJson(var) }}&quot;
  </code></pre>
  <p>I was able to loop through all existing Variables in the pipeline but it appears task variables are not added to this list?</p>
  <pre><code>Write-Host &quot;##vso[task.setvariable variable=filePath;isOutput=true]$YamlVar.FullName&quot;
  </code></pre>
  <p>Using the above task.setvariable I can set and access a variable but it's a single string, obviously to loop through a template a number of times and perform the same steps it's got to work with the <strong>${{ each var in variables }}:</strong> structure.</p>
  <p>Also managed to write a variables.yml and add it to an artifact but I can't seem to find a way to access my new artifact to potentially leverage.</p>
  <pre><code>- job: B
  dependsOn: A1
  pool:
    vmImage: windows-latest
  variables:
    - template: ./variables.yml
  steps:
    - ${{ each var in variables }}:
      - task: PowerShell@2
        displayName: 'Checking variables ${{ var.key }}'
        inputs:
          targetType: Inline
          script: |
            Write-Host ${{ var.name }}
            Write-Host ${{ var.key }}
            Write-Host ${{ var.value }}
            Write-Host &quot;${{ convertToJson(var) }}&quot;
  </code></pre>
  <p><strong>UPDATE</strong></p>
  <p>This is the closets answer so far, but it seem like when using.</p>
  <pre><code>- ${{ each var in variables }}:
  </code></pre>
  <p>The variable or parameter is not processed.so it's interpreted as is Micro $(allfiles) directly instead of expanded and leveraged.</p>
  <p><a href="https://stackoverflow.com/a/59451690/16318957">https://stackoverflow.com/a/59451690/16318957</a></p>
  <p>If I could some how just have my .yml artifact added to the build and then reference that it would work, but it appears I can't reference a template directly in a step that hasn't executed a download stage.</p>
  <p>This would allow me to create a template file with the parameters and variables hard coded then, publish the file and use that artifact to do the right amount of steps.</p>
  <p>When attempting to download my artifacts I'm not getting any files.</p>
  <p><a href="https://i.stack.imgur.com/B4Jfn.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/B4Jfn.png" alt="Artifact Download" /></a></p>
  <p><strong>UPDATE</strong></p>
  <p>I've solved this one, I essentially passed the object array into the template using the DevOps API to call a separate pipeline. I run a PowerShell script passing in the variables required.</p>
  <pre><code>- task: PowerShell@2
            inputs:
              filePath: './PowerShell/deployment.ps1'
              arguments: '-variable &quot;TEST&quot; -step &quot;TEST_DEPLOYMENT&quot; -env &quot;TEST&quot; -pat &quot;$(pat)&quot;'
  </code></pre>
  <p>Then essentially adjust the JSON variable and call the pipeline from the API with your array list. Your JSON needs to leverage objects as a JSON text.</p>
  <pre><code>$JSON = @'
  {
      &quot;variables&quot;:{
          &quot;file1&quot;:{
              &quot;isSecret&quot;:false,
              &quot;value&quot;:&quot;test11&quot;
          },
          &quot;file2&quot;:{
              &quot;isSecret&quot;:false,
              &quot;value&quot;:&quot;test21&quot;
          }
      },
      &quot;templateParameters&quot;:{
          &lt;zipfilefilenameshere&gt;,
      }
  }
  '@
  </code></pre>
  <p>Then trigger the pipeline with the API passing your object array.</p>
  <pre><code>uripipeline = &quot;https://dev.azure.com/$($org)/$($proj)/_apis/pipelines/$($id)/runs?api-version=6.0-preview.1&quot;
  </code></pre>
  <p>When you object is sent it needs to be structured like JSON text.</p>
  <p><strong>Example:</strong></p>
  <pre><code>&quot;[\&quot;file1.zip\&quot;,\&quot;file2.zip\&quot;]&quot;
  </code></pre>