1
votes

I can't figure out what is the problem with the code for a few days.. Can you guys have a look and say what I have done wrong? Organizaiton name and project name are put correct in the code.

This is the error message shown on PowerShell ISE:

Invoke-RestMethod : Cannot send a content-body with this verb-type. At \Project\DevopTask.ps1:47 char: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
1
As per your error, you cannot use Get method with a content body. You can pass query parameters with GET but no -Body argument. I am not familiar with WIQL but from the documentation, it looks looks like you are using the correct verb for what you are trying to achieve. That bein said, you would need to build your arguments into the query string, as per : docs.microsoft.com/en-us/rest/api/azure/devops/wit/queries/… - Sage Pourpre

1 Answers

0
votes

You just need to change the -Method (in the Invoke-RestMethod) from Get to Post.

You try to get Work Items with Work Item Query Language, in this Rest API you need to pass the WIQL in the body so the -Method should be Post.

See the docs here.