0
votes

Using the following docs, https://docs.microsoft.com/en-us/rest/api/azure/devops/wit/Work%2520Items/List?view=azure-devops-rest-5.0&viewFallbackFrom=vsts-rest-5.0, I have an api call that gets a list of workitems.

I convert that to Json in my devops build yml via a powershell script but looping through the "value" array doesn't seem to print anything to my devops console. Any help on syntax?

$jsonResp = $($workItemResponse | ConvertTo-Json -Depth 100)
Write-Host "json formatted response"
Write-Host $jsonResp.value

foreach($item in $($jsonResp.value))
{
        $releaseNotesHtml += "<div>" + $item.fields.System.Id + "</div>"
        Write-Host $item.fields.System.Id
        Write-Host $item.fields.System.Title
        Write-Host $item.fields.System.State
        Write-Host $item.fields.System.Description
}

I've also tried this, with no success.

foreach($item in $jsonResp.value)
{
            $releaseNotesHtml += "<div>" + $item.fields.System.Id + "</div>"
            Write-Host $item.fields.System.Id
            Write-Host $item.fields.System.Title
            Write-Host $item.fields.System.State
            Write-Host $item.fields.System.Description
}
1

1 Answers

0
votes

Use the following format:

$token = "<pat>"

$orgurl = "https://dev.azure.com/<org_name>/_apis/wit/workitems?ids=200,250,300&api-version=5.0"

$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))

$wiResponse = Invoke-RestMethod -Uri $orgurl -Headers @{Authorization = "Basic $token"} -Method Get -ContentType application/json

foreach($item in $wiResponse.value)
{
        $releaseNotesHtml += "<div>" + $item.id + "</div>"
        Write-Host $item.id
        Write-Host $item.fields.'System.Title'
        Write-Host $item.fields.'System.State'
        Write-Host $item.fields.'System.Description'
}

The result:

enter image description here