Share via

Access Denied error while triggering a pipeline using invoke-Rest method call in powershell task

Sheepu Kumar 0 Reputation points
2023-08-08T10:31:29.3366667+00:00

I want to trigger a pipeline using invoke-Rest Method call from powershell task of yml pipeline, but i am getting access denied error like below.

Access denied. DS
     | Common Build Service (xxxxx) needs Queue builds permissions for
     | build pipeline xxxx:xxxxxxx in
     | team project xxxxxxx to perform the action. For more information,
     | contact the Azure DevOps

Here is the code that I am using:


    - task: PowerShell@2
      enabled: true
      inputs:
        targetType: 'inline'
        script: |

          $definitionId = xxxxx
          $organization = "xxxx"
          $project = "xxxxx"

          $body = @{
            resources = @{
                pipelines = @{
                    pipeline = $definitionId
                    refName = "refs/heads/xxxxx"  # Branch to trigger the pipeline
                }
            }
          } | ConvertTo-Json
        
          $url = "https://dev.azure.com/$organization/$project/_apis/build/builds?definitionId=xxxxx&api-version=7.0"
                  Write-Host "URL: $url"
                  $pipeline = Invoke-RestMethod -Uri $url -Headers @{
                      Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
                      "Content-Type" = "application/json"
                  } -Method Post -Body $body
                  Write-Host "Pipeline = $($pipeline | ConvertTo-Json -Depth 100)"
      env:
        SYSTEM_ACCESSTOKEN: $(System.AccessToken)

Here I am using "system.AccessToken" as PAT token.
What will be the cause of the access denied failure...can someone guide me.

Community Center | Not monitored
0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.