0

I am using this code to update the build pipeline task by updating the json by this line $TaskDetail.enabled = "false" and posting the updated json:

$BuildName = "Test-demo"

$buildTaskName = 'Print Hello World'

$BuildDefinitions = Invoke-WebRequest -Headers $headers -Uri ("{0}/_apis/build/definitions?api-version=5.0" -f $TFSProjectURL)

$BuildDefinitionsDetail = convertFrom-JSON $BuildDefinitions.Content

foreach($BuildDetail in $BuildDefinitionsDetail.value){
    if($BuildDetail.name -eq $BuildName)
    {
        $Id = $BuildDetail.id
        $name = $BuildDetail.name
        $Project = $BuildDetail.project.name
        $BuildTask = Invoke-WebRequest -Headers $headers -Uri ("{0}/_apis/build/definitions/$($Id)?api-version=5.0" -f $TFSProjectURL)

        $BuildTaskDetails = convertFrom-JSON $BuildTask.Content

        foreach($TaskDetail in $BuildTaskDetails.process.phases.steps){

            if($TaskDetail.displayName -eq $buildTaskName)
            {
                $taskName = $TaskDetail.displayName
                $TaskDetail.enabled = "false"
            }
        }
        Write-Host $BuildTaskDetails
        $Updatedbuilddef = ConvertTo-Json $BuildTaskDetails
        buildUri = "$TFSProjectURL//_apis/build/definitions/$Id?api-version=5."
        $buildResponse =Invoke-WebRequest -Headers $headers -Uri $buildUri -Method Put -ContentType "application/json" -Body $Updatedbuilddef
    }
}

But I am getting this error:

Invoke-RestMethod : {"$id":"1","innerException":null,"message":"Processing of the HTTP request resulted in an exception. Please see the HTTP response returned by the 'Response' property of this exception for details.","typeName":"System.Web.Http.HttpResponseException, System.Web.Http","typeKey":"HttpResponseException","errorCode":0,"eventId":0}
At C:\Users\Z004APNA\Desktop\BuildPipelineScript\BuildDefinition_edit.ps1:145 char:26

  • ... dResponse = Invoke-RestMethod -Uri $buildUri -Method Post -Headers $h ...
  •             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
    
  • CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
  • FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
Vince Bowdren
  • 8,326
  • 3
  • 31
  • 56
Avin Verma
  • 11
  • 1
  • 6
  • Your API version has a typo in it. `5.`, not `5.0` or a more recent version. You should also probably try putting a `-Depth 5` and `-Compress` on your `ConvertTo-JSON`. – Daniel Mann Jun 28 '22 at 12:43
  • @DanielMann updated the API version 6.0 and using -Depth 5 and -Compress Getting this error Invoke-WebRequest : {"$id":"1","innerException":null,"message":"Value cannot be null.\r\nParameter name: definition.Steps[0]","typeName":"System.ArgumentNullException, mscorlib","typeKey":"ArgumentNullException","errorCode":0,"eventId":0} At C:\Users\Z004APNA\Desktop\BuildPipelineScript\test.ps1:64 char:26 + ... dResponse = Invoke-WebRequest -Headers $headers -Uri $buildUri -Metho ... – Avin Verma Jun 28 '22 at 13:11
  • + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand – Avin Verma Jun 28 '22 at 13:14
  • My pipeline is having 7 task I need to enable or disable the MSBuild task Is there any solution or another api to enable or disable a single task in my pipeline – Avin Verma Jun 28 '22 at 13:23
  • Editing the pipeline JSON via REST is an acceptable approach for classic pipelines. The best option, however, is to just use YAML pipelines where you can easily edit the pipeline definition as code in your repo. For your specific issue, ensure you're using a consistent API version to retrieve and update the JSON -- retrieving with v5.0 and updating with v6.0 probably won't work. – Daniel Mann Jun 28 '22 at 13:51

1 Answers1

0

Test your code sample and it has some issues.

1.In your code, it is missing authentication credentials. You can try to create PAT and use it in authentication .

2.When you use ConvertTo-Json, you need to add depth parameter to expand the json body.

3.For the buildurl, you need to modify the id format in the url.

$TFSProjectURL/_apis/build/definitions/$($Id)?api-version=5.0

Here is the example:

$token = "PAT"

$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))


$BuildName = "Test-demo"

$buildTaskName = 'Print Hello World'

$BuildDefinitions = Invoke-WebRequest -Headers @{Authorization = "Basic $token"} -Uri ("{0}/_apis/build/definitions?api-version=5.0" -f $TFSProjectURL)

$BuildDefinitionsDetail = convertFrom-JSON $BuildDefinitions.Content

foreach($BuildDetail in $BuildDefinitionsDetail.value){
    if($BuildDetail.name -eq $BuildName)
    {
        $Id = $BuildDetail.id
        $name = $BuildDetail.name
        $Project = $BuildDetail.project.name
        $BuildTask = Invoke-WebRequest -Headers @{Authorization = "Basic $token"} -Uri ("{0}/_apis/build/definitions/$($Id)?api-version=5.0" -f $TFSProjectURL)

        $BuildTaskDetails = convertFrom-JSON $BuildTask.Content

        foreach($TaskDetail in $BuildTaskDetails.process.phases.steps){

            if($TaskDetail.displayName -eq $buildTaskName)
            {
                $taskName = $TaskDetail.displayName
                $TaskDetail.enabled = "false"
            }
        }
        Write-Host $BuildTaskDetails
        $Updatedbuilddef = ConvertTo-Json $BuildTaskDetails -Depth 99
        buildUri = "$TFSProjectURL/_apis/build/definitions/$($Id)?api-version=5.0"
        $buildResponse =Invoke-WebRequest -Headers @{Authorization = "Basic $token"} -Uri $buildUri -Method Put -ContentType "application/json" -Body $Updatedbuilddef
    }
}
Kevin Lu-MSFT
  • 20,786
  • 3
  • 19
  • 28
  • Hi @kevin I am using the authentication credential param( [Parameter(Mandatory=$false)] [string] $Username = "Avin_test_PAT", [Parameter(Mandatory=$false)] [string] $PAT = "ylfjtmc5wqo2nmpxzp7b6gyojqnebyhm4tw3uj6gs5yhusbwb2ga", [Parameter(Mandatory=$false)] [string] $TFSProjectURL , ) $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $Username,$PAT))) $headers = @{ "Authorization" = ("Basic {0}" -f $base64AuthInfo) "Accept" = "application/json" } – Avin Verma Jul 02 '22 at 07:03
  • Thankyou so much now it is working fine using your changes I am able to do that – Avin Verma Jul 02 '22 at 07:04
  • Hi @Kevin Lu-MSFT while updating How to add comment? – Avin Verma Jul 04 '22 at 08:36
  • Where do you want to add comment? Do you mean that you want to update my answer? – Kevin Lu-MSFT Jul 04 '22 at 08:44
  • No after calling the api for updating the task it will update the task in the build pipeline, then in the history section it shows there with no comments, I want to add comment in that history – Avin Verma Jul 04 '22 at 08:52
  • Hi @Kevin Lu any hint or solution for comment to post while saving through script – Avin Verma Jul 11 '22 at 04:10