VSTS Rest API from Inline Powershell

Tips and tricks Inline Powershell task VSTS, call the VSTS/TFS Rest APIs from Powershell

Maximize how you use your VSTS build and release pipeline with Inline Powershell tasks. In this blog series ‘Tips and Tricks for Inline Powershell’, I will show simple samples on how to get more out of your pipelines. This blog post: VSTS Rest API

VSTS Inline Powershell task
The Inline PowerShell VSTS task enables you to execute PowerShell from a textbox within your build or release pipeline. You can run a PowerShell script on you agent or as Azure Powershell.
Introduction Inline Powershell Task
Install Inline Powershell Task

The VSTS Rest API let you access and change information in your projects. You can start a build, release or change workitems. To access the Rest API from you Powershell you can use a personal access token or use OAUTH token provide in the build. You have to enable Allow script access OAuth Token in you pipeline configuration:

Now you are ready to rock. To access the current build steps you can use the following script:

[String]$buildID = "$env:BUILD_BUILDID"
[String]$project = "$env:SYSTEM_TEAMPROJECT"

$headers = @{ Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN" }
$url= $projecturi + $project + "/_apis/build/builds/" + $buildID + "/timeline?api-version=2.0"

Write-Host $url
$responseBuild = Invoke-RestMethod -Uri $url -headers $headers -Method Get | select records

foreach ($record in $responseBuild.records)
  $result = $record.result
  $taskname = $record.name
  Write-Host "task $taskname: $result"

The Rest API will return a list with the status of the tasks. This will let you react on information from the current build. The next sample will create a workitem in you team project:

[String]$project = "$env:SYSTEM_TEAMPROJECT"

$headers = @{ Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN" }
$url= $projecturi + $project + '/_apis/wit/workitems/$bug?api-version=2.0'

    `"op`": `"add`",
    `"path`": `"/fields/System.Title`",
    `"value`": `"workitem create test`"

Write-Host "$url"

$response= Invoke-RestMethod -Uri $url  -ContentType "application/json-patch+json" -Body $body -headers $headers -Method PATCH   

Write-Host $response

If you have any tests in your build and you detect quality degradation, create a issue on your backlog as reminder.

More tips and tricks
Use VSTS variables
Let your task fail
Set progress
Change buildnumber
VSTS Command overview
Call a WebHook
Download a file
Install a Powershell Module
Navigate VSTS as filesystem
Make VSTS API Rest calls
Script example: Act on failed build

VSTS Rest API Overview
VSTS Rest API Samples


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: