Home > Software engineering >  create multiple projects error Azure DevOps rest api
create multiple projects error Azure DevOps rest api

Time:06-24

I am using a rest API powershell to create multiple projects in azure devops. Writing my script in below way but it fails. Any guidance on how I can construct my script to pick up multiple names?

'
    $connectionToken="token"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
$ProjectUrl = "url 
$Body = @{
        name = @{'TestProjectCreation1','test1','test2'

}
        description = 'Frabrikam travel app for Windows Phone'
        capabilities = @{
          versioncontrol = @{
            sourceControlType = 'Git'
          }
          processTemplate = @{
            templateTypeId = 'template id'
          }
        }
      } | ConvertTo-Json
$projectscreated = Invoke-RestMethod -Uri $ProjectUrl -Method Post -ContentType "application/json"  -Headers @{
    Authorization=("Basic {0}" -f $base64AuthInfo)} -Body $Body

'

CodePudding user response:

According to your description, you could create a new array for projects and foreach the project to create: scrpit like:

$url='https://dev.azure.com/{org}/_apis/projects?api-version=6.0'
$connectionToken="{PAT}"
$base64AuthInfo = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))

$projects= @('TestProjectCreation1','test1','test2')
Foreach($prject in $projects){

$body="
  {
    `"name`": `"$prject`",
    `"capabilities`": {
      `"versioncontrol`": {
      `"sourceControlType`": `"Git`"
    },
    `"processTemplate`": {
      `"templateTypeId`": `"6b724908-ef14-45cf-84f8-768b5384da45`"
    }
  }
  }
"
$response= Invoke-RestMethod -Uri $url  -ContentType "application/json" -Body $body -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method POST
}
  • Related