简体   繁体   English

尝试使用Powershell在VSTS中创建工作项时出现Bad Request错误

[英]Bad Request error while trying to create a work item in VSTS using Powershell

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. 我尝试了多种方法来解决此错误,我从我的VSTS帐户创建了PAT,并将其包含在脚本中。 However, the call to the REST API returns saying "The remote server returned an error: (400) Bad Request". 但是,对REST API的调用返回说“远程服务器返回错误:(400)错误的请求”。

Using the same PAT gets me the information from VSTS using the GET method but it is not creating a work item. 使用相同的PAT可以使用GET方法从VSTS中获取信息,但它不会创建工作项。

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 正文从CSV获取其值并存储在$ 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如下

$Uri = " https://[xxxx].visualstudio.com/InfraEng/_apis/wit/workitems/ `$product backlog item?api-version=1.0" $ 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 当我尝试触发Invoke-RestMethod时捕获响应时,得到以下信息

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; IsMutuallyAuthenticated:错误的Cookie:{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-0000-000000000000%22%7D}标头:{Pragma,X-TFS-ProcessId,Strict-Transport-Security,ActivityId ...} SupportsHeaders:真实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 charset = utf-8 CharacterSet:utf-8服务器: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方法:POST IsFromCache:False

I don't necessarily think it is a problem with your Authorization. 我不一定认为您的授权有问题。 I'd expect you to probably get a different error, especially if you are able to use the header on a GET. 我希望您可能会收到其他错误,特别是如果您能够在GET上使用标头。

This works for me: 这对我有用:

$Cred = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(":$pat"))
$Headers = @{
    Authorization = ("Basic {0}" -f $Cred)
}

Invoke-RestMethod -Uri 'https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/$Test Case?api-version=4.1' -Method PATCH -Header $Headers -Body '[{"op": "add","path": "/fields/System.Title","from": null,"value": "Sample test case"}]' -ContentType 'application/json-patch+json'

I think your issue is with the $Body but that is not included in the question. 我认为您的问题出在$Body但这不包含在问题中。

Check out this page - https://docs.microsoft.com/en-us/rest/api/azure/devops/wit/work%20items/create?view=azure-devops-rest-5.0 签出此页面-https://docs.microsoft.com/zh-CN/rest/api/azure/devops/wit/work%20items/create?view=azure - devops - rest - 5.0

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Azure上使用Powershell为PipelineFailedRun创建警报时出现错误请求错误 - Bad Request error while creating Alerts for PipelineFailedRuns using Powershell on Azure 使用PowerShell在带有REST API的VSTS中创建工作项? - Create work items in vsts with rest api using powershell? 使用PowerShell创建TFS工作项 - Create TFS work item with PowerShell 尝试通过具有Power User特权的Powershell创建用户时出错 - Error while trying to create user through Powershell with the privilege of Power User 如何使用PowerShell在VSO项目中创建工作项 - How to create work item in VSO project using PowerShell 将 nsg 流日志传输到 azure 中不同订阅中的存储帐户时,在 powershell 中出现“错误请求”错误 - getting “Bad Request” error in powershell while transferring the nsg flow log to the storage account in different subscription in azure 尝试通过PowerShell创建新工作项时,项目集合获取服务未返回任何值 - Project collection get service returning no value when trying to create a new work item via PowerShell 使用新项目创建文件夹偶尔会在 Powershell 中引发错误 - Using new-item to create folder occasionally throws error in Powershell 尝试使用CSOM PowerShell创建网站集时发生错误 - Error when trying to create site collection using CSOM PowerShell Powershell:使用 REST API 从 Sharepoint 列表中删除项目 - 远程服务器返回错误:(400) 错误请求 - Powershell: Using REST API to Delete Items from a Sharepoint List - The remote server returned an error: (400) Bad Request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM