I have tried many ways to get around this error, I have created a PAT from VSTS account of mine and included this in the script. However, the call to the REST API returns saying "The remote server returned an error: (400) Bad Request".
Using the same PAT gets me the information from VSTS using the GET method but it is not creating a work item.
I'm providing the authentication by following steps
$Creds = [Text.Encoding]::ASCII.GetBytes(":$Token")
$Creds = [System.Convert]::ToBase64String($Creds)
$Headers = @{
Authorization = ("Basic {0}" -f $Creds)
}
and passing the rest by following steps
Invoke-RestMethod -Uri $Uri -Method POST -Headers $Headers -Body $Body -ContentType $ContentType
Body gets its values from a CSV and stored in $values
foreach ($value in $values)
{
$PBIName = $value.Name
$Resource = $value.Resource
$Body = "[
{
`"op`": `"add`",
`"path`": `"/fields/System.Title`",
`"value`": `"$($PBIName)`"
}
{
`"op`": `"add`",
`"path`": `"/fields/System.AreaPath`",
`"value`": `"InfraEng\DCO`"
}
{
`"op`": `"add`",
`"path`": `"/fields/System.IterationPath`",
`"value`": `"InfraEng`"
}
{
`"op`": `"add`",
`"path`": `"/fields/System.AssignedTo`",
`"value`": `"$($Resource)`"
}]"
| ConvertTo-Json
and URI is as follows
$Uri = "https://[xxxx].visualstudio.com/InfraEng/_apis/wit/workitems/`$product backlog item?api-version=1.0"
when I try catching the response when I trigger Invoke-RestMethod, I get the following
IsMutuallyAuthenticated : False Cookies : {VstsSession=%7B%22PersistentSessionId%22%3A%2248171d3c-4c0f-413f-9143-59e6e50047c3%22%2C%22PendingAuthenticationSessionId%22%3A%22 00000000-0000-0000-0000-000000000000%22%2C%22CurrentAuthenticationSessionId%22%3A%2200000000-0000-0000-0000-000000000000%22%7D} Headers : {Pragma, X-TFS-ProcessId, Strict-Transport-Security, ActivityId...} SupportsHeaders : True ContentLength : 373 ContentEncoding : ContentType : application/json; charset=utf-8 CharacterSet : utf-8 Server : LastModified : 1/9/2019 9:35:03 AM StatusCode : BadRequest StatusDescription : Bad Request ProtocolVersion : 1.1 ResponseUri : https://[xxxx].visualstudio.com/InfraEng/_apis/wit/workitems/$product backlog item?api-version=1.0 Method : POST IsFromCache : False