Search code examples
azurepowershellazure-devopsazure-pipelines

Getting error while trying to disable Azure DevOps pipeline using Powershell


I am trying to disable a build pipeline on Azure DevOps using Powershell but getting an error

Script:

$project = "<Project-Name>"

$organization = "<Org-Name>"
$pat = "<PAT>"

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($pat)"))


$headers = @{
    Authorization = "Basic $base64AuthInfo"
    "Content-Type" = "application/json"
}


$pipelineDetailsUrl = "https://dev.azure.com/$organization/$project/_apis/pipelines/<ID>?api-version=6.0"
$pipelineDetails = Invoke-RestMethod -Uri $pipelineDetailsUrl -Method Get -Headers $headers
$pipelineDetails.configuration.designerJson.queueStatus = "disabled"
$body = $pipelineDetails | ConvertTo-Json -Depth 10
Invoke-RestMethod -Uri $pipelineDetailsUrl -Method Put -Headers $headers -Body $body -ContentType "application/json"

However I am getting below error

Invoke-RestMethod : {"count":1,"value":{"Message":"The requested resource does not support http method 'PUT'."}}


Solution

  • The right endpoints to use are the following:

    Example

    $PROJECT="my-project"
    $ORGANIZATION="https://dev.azure.com/my-organization"
    $TOKEN="xxxxxxxxxxxxxxxxxxxxxx"
    
    $BUILD_DEFINITION_ID="123"
    
    # Base64-encode the Personal Access Token (PAT)
    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($TOKEN)"))
    
    # Same URL for getting the build definition and updating it
    $apiUrl = "{0}/{1}/_apis/build/definitions/{2}?api-version=7.2-preview.7" -f $ORGANIZATION, $PROJECT, $BUILD_DEFINITION_ID
    
    Write-Host "Endpoint: $apiUrl"
    
    $headers = @{
      Authorization=("Basic {0}" -f $base64AuthInfo)
      Accept="application/json"
      "Content-Type"="application/json"
    }
    
    $definition = Invoke-RestMethod -Uri $apiUrl -Headers $headers -Method GET
    # $definition.queueStatus = "enabled"
    # $definition.queueStatus = "paused"
    $definition.queueStatus = "disabled"
    
    $body = $definition | ConvertTo-Json
    
    $response = Invoke-RestMethod -Uri $apiUrl -Headers $headers -Method PUT -Body $body
    
    Write-Host $response