0

I have this pipeline, which calls a powershell script. The powershell script needs run for each values which is passed into it. So i have created a object in parameters called VariableFiles and mabye a foreach on the steps.


pool: 
  vmImage: windows-latest

parameters:
  - name: variableFiles
    displayName: Variable File
    type: object
    default:
      - test
      - testApp
  - name: environment
    displayName: Environment
    type: string
    default: Dev
    values:
      - Dev
      - Test
      - Stag 
      - Prod

variables:
 - group: ADO_statefile 

name: 'ADO Automation'

stages:
- stage: powershellDeployment
  displayName: "PowerShell Deployment"
  jobs:
  - deployment: "deployment"
    displayName: "PowerShell Deployment"
    environment: 'ado - Dev'
    strategy:
      runOnce:
        deploy:
          steps:
            - ${{ each variableFile in parameters.variableFiles }}:          
              - template: steps.yml
                parameters:
                  spn: $(service_connection)
                  JsonPath: ${{ variableFile }}
                  validate: true

When i have more the 1 object in the variableFiles parameter i get this error: ##[error]The term 'D:\a\1\s\ADO\powershell\CreateAADGroup\createAADGroup.ps1' is not recognized as a name of a cmdlet, function, script file, or executable program.

When I run the same script with just one object in the variableFiles parameter everything works.

I dont really get it.

Here is the steps.yml for template, which is called

steps:
  - checkout: self

  - task: PowerShell@2
    displayName: "List environment variables"
    inputs:
      pwsh: true
      targetType: "inline"
      script: |
        # List all pipeline variables
        dir env:

  - task: AzurePowerShell@5
    displayName: "create ADD groups"
    inputs:
        azureSubscription: ${{ parameters.spn }}
        pwsh: true
        azurePowerShellVersion: LatestVersion
        ScriptType: 'FilePath'
        ScriptPath: "$(Build.SourcesDirectory)/ADO/powershell/CreateAADGroup/createAADGroup.ps1"
        ScriptArguments: '-JsonPath ${{ parameters.JsonPath }}'        

and where is the powershell script:


Param(
    [string]$JsonPath
)


$parameters = Get-Content -Raw -Path "$($env:BUILD_SOURCESDIRECTORY)\ADO\terraform\$JsonPath.tfvars.json"  | ConvertFrom-Json
foreach ($GroupName in $parameters.AADGroupName) {
    # Check if the group already exists
    $existingGroup = Get-AzADGroup -DisplayName $GroupName -ErrorAction SilentlyContinue
    
    if ($existingGroup) {
        Write-Host "Group $GroupName already exists. Skipping creation."
    }
    else {
        Write-Host "Creating group: $GroupName"
        New-AzADGroup -DisplayName $GroupName -MailNickname $GroupName
    }
}

I have tried so many things, move values around and google everything around the subject. I am lost.

1 Answer 1

0

You are using

$(Build.SourcesDirectory)

as a ScriptPath in your yaml pipeline thus you are receiving term not recognized error.

The predefined variable for Build.SourcesDirectory is c:\agent_work\1\s where your source code files are downloaded in the agent, Instead use

$(System.DefaultWorkingDirectory)

System.DefaultWorkingDirectory will fetch the powershell script and your other files correctly from the root of your repository.

Your updated code should look like:-

steps:
  - checkout: self

  - task: PowerShell@2
    displayName: "List environment variables"
    inputs:
      pwsh: true
      targetType: "inline"
      script: |
        # List all pipeline variables
        dir env:

  - ${{ each variableFile in parameters.variableFiles }}:
    - task: AzurePowerShell@5
      displayName: "create ADD groups"
      inputs:
        azureSubscription: ${{ parameters.spn }}
        pwsh: true
        azurePowerShellVersion: LatestVersion
        ScriptType: 'FilePath'
        ScriptPath: '$(System.DefaultWorkingDirectory)/ADO/powershell/CreateAADGroup/createAADGroup.ps1'
        ScriptArguments: '-JsonPath ${{ variableFile }}'

I tried running Get-AzADGroup and New-AzADGroup command by storing them in a script in my repository and used the System.DefaultWorkingDirectory as a script path to the script file and the command and pipeline ran successfully, Refer below:-

trigger:
- main

pool:
  vmImage: ubuntu-latest

steps:
- script: echo Hello, world!
  displayName: 'Run a one-line script'

- script: |
    echo Add other tasks to build, test, and deploy your project.
    echo See https://aka.ms/yaml
  displayName: 'Run a multi-line script'

- task: AzurePowerShell@5
  inputs:
    azureSubscription: 'devopsappsilicon'
    ScriptType: 'FilePath'
    ScriptPath: '$(System.DefaultWorkingDirectory)/azadgrp1.ps1'
    azurePowerShellVersion: 'LatestVersion'

Output:-

enter image description here

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.