Getting Work Item data in powershell through REST API
VSTS and the latests versions of on-premise TFS has the ability to access data through REST API . This way to access TFS data is really convenient expecially 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 request with simple Basic Authentication. Here is a PowerShell snippet that retrieve a Work Item with a given ID.
|
|
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 a 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
|
|
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
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):
|
|
Thanks to PowerShell ISE you can also use intellisense to have all the properties convienently listed.
Figure 3: Intellisense on returned object
Gian Maria