Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Getting Work Item Data in Powershell Through REST API

DZone's Guide to

Getting Work Item Data in Powershell Through REST API

Here's a comprehensive look at extracting​ work item data with a REST API through Powershell.

Free Resource

Migrating from On-Prem to Cloud Middleware? Here’s what Aberdeen Group says leading companies should be considering. Brought to you in partnershp with Liaison Technologies

VSTS and the latest versions of on-premise TFS have the ability to access data through REST API. This way to access TFS data is really convenient especially if used from PowerShell scripts, because you do not need any external dependency, except being able to issue REST requests with the Invoke-RestRequest cmdlet.

To simplify accessing your VSTS account, you can enable alternate credentials, needed to issue a request with simple Basic Authentication. Here is a PowerShell snippet that retrieve a Work Item with a given ID.


$username = "alternateusername"
$password = "alternatepassword"

$basicAuth = ("{0}:{1}" -f $username,$password)
$basicAuth = [System.Text.Encoding]::UTF8.GetBytes($basicAuth)
$basicAuth = [System.Convert]::ToBase64String($basicAuth)
$headers = @{Authorization=("Basic {0}" -f $basicAuth)}

$result = Invoke-RestMethod -Uri https://gianmariaricci.visualstudio.com/defaultcollection/_apis/wit/workitems/100?api-version=1.0  -headers $headers -Method Get

This is a simple code you can find everywhere on the internet, it is just a matter of converting username and password in Base64 string and pass the result to Invoke-RestMethod with the –headers argument. The result is an object parsed from the JSON returned by the call. The nice aspect of Powershell is that you can simply use the Get-Member cmdlet to list properties of returned object. Here is the result of instruction


$result | Get-Member

image








Figure 1: result of the Get-Member shows all properties of returned object

In this situation, we can see the id of the Work Item and the Rev property, but probably all the data we need is inside the fields property. If we simply Write-Output the $result.fields we got the full list of all the fields of the returned Work Item

image

Figure 2: content of fields property of returned Work Item

Any property can be accessed with standard Dot Notation (but pay attention to the dot in the name of the property, fields property is actually an HashSet):


$closedBy = $result.fields.'Microsoft.VSTS.Common.ClosedBy'
Write-Output "Work Item was closed by: $closedBy"

Thanks to PowerShell ISE you can also use IntelliSense to have all the properties conveniently listed.

image








Figure 3: IntelliSense on returned object

Is iPaaS solving the right problems? Not knowing the fundamental difference between iPaaS and iPaaS+ could cost you down the road. Brought to you in partnership with Liaison Technologies.

Topics:
rest ,powershell

Published at DZone with permission of Ricci Gian Maria, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}