This script works well as long as the build server has permission to read and update test plans. But in some environments (such as when running on a hosted build agent in VSO) we need to do an explicit authentication. The solution I’ve choosen is to simply pass in credentials to the script and do an authentication before using the TFS API.

The script now takes two optional parameters if you want to authenticate, username and password, and you can just pass them in from the build definition. You can use ether basic authentication and pass both or use the safer option and pass a Personal Access Token (PAT) in which case you only pass the PAT as the password.

Here’s an example where you go to manage your credentials in Visual Studio Online. First go to your profile:

From the Security tab you can either create a Personal Access Token:

Press Add to create an access token:

Copy the PAT (it cannot be retrieved later):

If you want to use basic authentication go to the Alternate authentication section and fill in the secondary username and password: