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