Home > Enterprise >  Unable to delete DevOps branch using REST API in PowerShell
Unable to delete DevOps branch using REST API in PowerShell

Time:12-06

I am trying to delete Azure DevOps branch by following the steps in the link. https://docs.microsoft.com/en-us/rest/api/azure/devops/git/refs/update-refs?view=azure-devops-rest-4.1

Note: Branch does not have any policy/lock associated.

I can test API in postman and its success. However , the same API is not working in PowerShell.

I am trying to upload csv file where columns are "Branchname", "OldObjectId".

Error: The remote server returned an error: (400) Bad Request

    $FileData = Get-Content $filePath | Select -skip 1 | ConvertFrom-Csv -Header  "BranchName","objectId"

     ## Processing Each Row of Excel Data
$FileData | ForEach-Object {
    Write-Output " Deleting branch:$($_.BranchName)"

   ##Assigning "0000000000000000000000000000000000000000" to newObjectId makes branch to delete.
    $body = @{
       oldObjectId =$($_.objectId)
        newObjectId = "0000000000000000000000000000000000000000"
       name = $($_.BranchName)
    } 
    $json= ConvertTo-Json @( $body ) -Depth 100
    Write-Output $DeleteBranche_BaseURL
    Write-Output $json

    ##Innvoking REST API  to delete stale branch
    $ADO_ADODeleteBranchesResponse = Invoke-RestMethod  -Uri $DeleteBranche_BaseURL  -Method POST -Headers $AzureDevOpsAuthenicationHeader  -Body $json -ErrorAction Ignore
    Write-Output #ADO_ADODeleteBranchesResponse
  }

CodePudding user response:

Can you check your content type for the request? I use -ContentType "application/json" in the same requests:

Invoke-RestMethod -Uri $PostUrl -Method Post -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}  -Body $body
  • Related