-1

I have a yaml pipeline that's simply used to run a script on a subscription. I use the AzurePowerShell@5 task for this.

The problem is I have 22 other subscriptions I need to run the same script on using the exact same script parameters. Only thing different will be the target azureSubscription field. Instead of having 22 blocks of the same task with different subscription names, is there a faster way? Can I throw the names of the 22 subscriptions in an array in the variables section and loop through them somehow? Please advise

stages:

- stage: Deploy
  displayName: 'Deploy Pipeline - Access Worker'

  jobs:
  - job: AccessWorker
    displayName: 'Access Worker'

    steps:
    - download: current
      artifact: 'AccessWorker'

    - task: AzurePowerShell@5
      displayName: 'Access Worker'
      inputs:
        azurePowerShellVersion: LatestVersion
        azureSubscription: 'My Azure Subscription 1'
        scriptPath: '$(Pipeline.Workspace)/AccessWorker/.src/Access_Worker/access_worker.ps1'
        scriptArguments: >
          -RoleDefinitionName 'Reader'
          -TagName 'Owner'

    - task: AzurePowerShell@5
      displayName: 'Access Worker'
      inputs:
        azurePowerShellVersion: LatestVersion
        azureSubscription: 'My Azure Subscription 2'
        scriptPath: '$(Pipeline.Workspace)/AccessWorker/.src/Access_Worker/access_worker.ps1'
        scriptArguments: >
          -RoleDefinitionName 'Reader'
          -TagName 'Owner'

    - task: AzurePowerShell@5
      displayName: 'Access Worker'
      inputs:
        azurePowerShellVersion: LatestVersion
        azureSubscription: 'My Azure Subscription 3'
        scriptPath: '$(Pipeline.Workspace)/AccessWorker/.src/Access_Worker/access_worker.ps1'
        scriptArguments: >
          -RoleDefinitionName 'Reader'
          -TagName 'Owner'
DivZ
  • 678
  • 12
  • 20
  • Did you start by looking at the documentation? – Daniel Mann Dec 19 '21 at 23:27
  • @DanielMann I did. Most of the examples in the documentations show how to loop within a property itself (like inside scriptArguments or scriptPath) but I haven't figured out how to loop through those properties itself. I'm gonna keep trying though I feel like I'm on the right track. – DivZ Dec 20 '21 at 00:02

1 Answers1

1

I figured it out - it was pretty easy. Here's the solution:

parameters:
  - name: subscriptions
    type: object
    default:
      - 'My Azure Subscription 1'
      - 'My Azure Subscription 2'
      - 'My Azure Subscription 3'

stages:
  - stage: Deploy
    displayName: "Deploy Pipeline - Get Resource Count"

    jobs:
      - job: ResourceCount
        displayName: "Subscription Loop Test"

        steps:
          - ${{ each subscription in parameters.subscriptions }}:
            - task: AzurePowerShell@5
              displayName: "Get Resource Count"
              inputs:
                azurePowerShellVersion: LatestVersion
                azureSubscription: ${{ subscription }}
                scriptType: InlineScript
                Inline: |
                  Write-Host "Subscription Name: $((Get-AzContext).Subscription.Name)"
                  Write-Host "Resource Count: $((Get-AzResource).Count)"

DivZ
  • 678
  • 12
  • 20