简体   繁体   中英

create multiple projects error Azure DevOps rest api

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

'

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM