【发布时间】:2019-05-26 03:52:50
【问题描述】:
这几天我不知道代码有什么问题.. 你们可以看看并说我做错了什么吗? 组织名称和项目名称在代码中正确放置。
这是 PowerShell ISE 上显示的错误消息:
Invoke-RestMethod:无法发送具有此动词类型的内容主体。在 \Project\DevopTask.ps1:47 字符:17 + ... $response = Invoke-RestMethod -Uri $url -headers $headers -Method Get ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Invoke-RestMethod], ProtocolViolationException + FullyQualifiedErrorId:System.Net.ProtocolViolationException,Microsoft.PowerShell.Commands.InvokeRestMet hodCommand
$collectionuri = $Env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI
$token = $Env:SYSTEM_ACCESSTOKEN # need to configure build to allow passing OAuth tokens
$basicAuth = "{0}:{1}" -f '', 'token~~fdsafdsa'
$basicAuth = [System.Text.Encoding]::UTF8.GetBytes(":$($basicAuth)")
$basicAuth = [System.Convert]::ToBase64String($basicAuth)
$headers = @{Authorization=("Basic {0}"-f $basicAuth)}
$WorkItemType = 'Recently updated'
$url = $collectionuri + 'https://dev.azure.com/{OrganizationName}/{ProjectName}/_apis/wit/queries/test/wiql?api-version=5.0'
$WIQL_query = "select [System.Id], [System.WorkItemType], [System.Title], [System.AssignedTo], [System.State], [System.IterationPath] from WorkItems where [System.TeamProject] = @project and [System.Id] in (@recentProjectActivity) and not [System.State] in ('Closed', 'Inactive', 'Completed') and [System.IterationPath] Under 'Sprint number two' order by [System.ChangedDate] desc"
$body = @{ query = $WIQL_query }
$bodyJson=@($body) | ConvertTo-Json
$response = Invoke-RestMethod -Uri $url -headers $headers -Method Get -ContentType "application/json" -Body $bodyJson
$workitems = $response.workItems
Write-Host "Found" $workitems.Count "work items of type:" $WorkItemType
【问题讨论】:
-
根据您的错误,您不能将 Get 方法与内容正文一起使用。您可以使用 GET 传递查询参数,但没有 -Body 参数。我不熟悉 WIQL,但从文档来看,看起来您正在使用正确的动词来实现您想要实现的目标。也就是说,您需要将参数构建到查询字符串中,如下所示:docs.microsoft.com/en-us/rest/api/azure/devops/wit/queries/…
标签: powershell azure-devops azure-devops-rest-api