skip to Main Content

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’."}}

3

Answers


  1. Chosen as BEST ANSWER

    Update

    Found the problem, it was the endpoint I was trying to hit, instead of

    "https://dev.azure.com/$organization/$project/_apis/pipelines/<ID>?api-version=6.0"
    

    use

    "https://dev.azure.com/$organization/$project/_apis/build/definitions/<ID>?api-version=6.0"
    

  2. The error message says that the Azure DevOps REST API does not support the HTTP PUT method for the endpoint you’re using. To disable a build pipeline, you should use the correct endpoint and method supported by the Azure DevOps API. Like this:

    $project = "<Project-Name>"
    $organization = "<Org-Name>"
    $pipelineId = "<Pipeline-ID>"  # Update with the actual pipeline ID
    $pat = "<PAT>"
    
    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($pat)"))
    
    $headers = @{
        Authorization = "Basic $base64AuthInfo"
        "Content-Type" = "application/json"
    }
    
    # Get the pipeline details
    $pipelineDetailsUrl = "https://dev.azure.com/$organization/$project/_apis/pipelines/$pipelineId?api-version=6.0-preview.1"
    $pipelineDetails = Invoke-RestMethod -Uri $pipelineDetailsUrl -Method Get -Headers $headers
    
    # Set the pipeline to disabled
    $pipelineDetails.configuration = $pipelineDetails.configuration | ForEach-Object {
        $_.enabled = $false
        $_
    }
    
    $body = $pipelineDetails | ConvertTo-Json -Depth 10
    
    # Update the pipeline with the new configuration
    $updatePipelineUrl = "https://dev.azure.com/$organization/$project/_apis/pipelines/$pipelineId?api-version=6.0-preview.1"
    Invoke-RestMethod -Uri $updatePipelineUrl -Method Put -Headers $headers -Body $body -ContentType "application/json"
    

    The script first retrieves the pipeline details using a GET request.
    It then modifies the enabled property of the pipeline configuration to false.
    Finally, it sends a PUT request with the updated pipeline details to disable the pipeline.

    Login or Signup to reply.
  3. 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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search