skip to Main Content

How can I create multiple pipelines in Microsoft Azure DevOps by calling objects from single or multiple csv files?

How can the script pick up blank space from one of the excel columns? For example Folder Path

This is what I have so far.

Import-Csv -Path "C:Users*******Downloadsexcelnotes.csv" 

$pipelineName = $pipelineVariables.name
$pipelineFolderPath = $pipelineVariables.FolderPath
$pipelineDescription = $pipelineVariables.Description
$pipelineRepostiory = $pipelineVariables.Repository
$pipelineBranch = $pipelineVariables.Branch
$pipelineYamlPath = $pipelineVariables.YamlPath


az pipelines create --name $pipelineName --folder-path $pipelineFolderPath --description $pipelineDescription --repository $pipelineRepostiory --branch $pipelineBranch --yml-path $pipelineYamlPath

The value for variables.

$pipelineName = $pipelineVariables.name
$pipelineFolderPath = $pipelineVariables.FolderPath
$pipelineDescription = $pipelineVariables.Description
$pipelineRepostiory = $pipelineVariables.Repository
$pipelineBranch = $pipelineVariables.Branch
$pipelineYamlPath = $pipelineVariables.YamlPath


Name        : test-ci
FolderPath  : build
Description : test ci pipeline
Repository  : https://dev.azure.com/******/*****/_git/******
Branch      : prod
Yaml Path   : ./yaml/build.yml

This is the error message that I receive.

az : ERROR: argument --folder-path: expected one argument
At line:1 char:1
+ az pipelines create --name $pipelineName --folder-path $pipelineFolde ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (ERROR: argument...ed one argument:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

This is my csv file

enter image description here

2

Answers


  1. If your CSV-file is stored in the variable $pipelineVariables then to pick up the Folder Path or Yaml Path (with whitespaces), you would simply:

    $pipelineFolderPath = $pipelineVariables."Folder Path"
    $pipelineYamlPath = $pipelineVariables."Yaml Path"
    

    Example

    Here is a quick example with your CSV-file and data:

    1. First, by just importing it into a variable, I can see that in my case it did not properly create it into a Powershell array of objects:
      enter image description here

    2. To solve this -Delimiter ";" is being used:
      enter image description here

    3. Example code snippet with foreach loop:

    $Csv = Import-Csv .CreatePipelineExample.csv -Delimiter ";"
    
    foreach ($Pipeline in $Csv) {
    
      az pipelines create --name $Pipeline.Name --folder-path $Pipeline."Folder Path" --description $Pipeline.Description --repository $Pipeline.Repository --branch $Pipeline.Branch --yml-path $Pipeline."Yaml Path"
    }
    

    I hope this helps!
    Good luck!

    Login or Signup to reply.
  2. You can use -Delimiter "," not ; for csv import.

    In addition, please also add --project for your az pipelines create command.

    1. Use command to login: echo PAT | az devops login --organization https://dev.azure.com/orgname.

    2. With below PowerShell script:

    # Import CSV file
    $csvData = Import-Csv -Path "C:UsersAdministratorDownloadsexcelnotes.csv" -Delimiter ","
    
    # Loop through each row in the CSV and create a pipeline
    foreach ($pipeline in $csvData) {
        # Output each value
        Write-Output "pipelineName: $($pipeline.Name)"
        Write-Output "pipelineFolderPath: $($pipeline.'Folder Path')"
        Write-Output "pipelineDescription: $($pipeline.Description)"
        Write-Output "pipelineRepository: $($pipeline.Repository)"
        Write-Output "pipelineBranch: $($pipeline.Branch)"
        Write-Output "pipelineYamlPath: $($pipeline.'Yaml Path')"
    
        az pipelines create --name $($pipeline.Name) --folder $($pipeline.'Folder Path') --description $($pipeline.Description) --repository $($pipeline.Repository) --branch $($pipeline.Branch) --yml-path $($pipeline.'Yaml Path') --project {projectname}
        Write-Output "------------------------"
    
    }
    

    The script output:

    enter image description here

    The pipelines created:

    enter image description here

    PS: in my script, you can even remove -Delimiter "," for csv import, it’s also working.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search