Day 58: PowerShell and Visual Studio Online

Welcome to Day 58 of the “100 Days of DevOps with PowerShell”! For background on our goals in this series, see Announcing the “100 Days of Devops with PowerShell” Series here at SCC.

In some of my previous posts, I discussed how to use PowerShell and the Team Foundation Server API to work with an on-site version of Team Foundation Server.  There is also a version of Team Foundation Server in the cloud, hosted by Microsoft, called Visual Studio Online, at www.visualstudio.com, that is starting to see more traction with clients. However, the API tricks that I showed in the previous posts won’t work with the online version of Team Foundation Server. Instead, Microsoft has provided a new API for Visual Studio Online, based on REST, OAUTH, Json, and Service Hooks, all of which are standard web technologies broadly supported in the industry. The main reason they went this route was to make it easier to integrate with other web-based tools and mobile devices.  In this post, I’m going to show you the bare basics of how to get started using PowerShell to access Visual Studio Online.

Enable Alternate Authentication Access

Normally, when I access Visual Studio Online, I’m presented a web page where I enter my credentials.  However, when creating a PowerShell script for performing some function, this is not ideal.  So, the first step toward using PowerShell to access Visual Studio Online is to enable alternate authentication access on our Visual Studio Online account.  Once that is enabled, I can then write code to access Visual Studio Online, using the alternate credentials.

To create the alternate credentials, open your Visual Studio Online account in a web browser.   Click on your name in the top right corner of the page, and select My profile.

image

 

This opens the User Profile Dialog window. Click the Credentials tab, then click the Enable alternate credentials link.

image

 

Provide a user name and password for your alternate credentials, and click the Save Changes button.

image

At this point, you have now enabled your alternate credentials.  The next step is to create a PowerShell script for accessing the REST API.  This is where PowerShell 4.0 comes to the rescue.

Connecting Using PowerShell 4.0

PowerShell 4.0 provides a new cmdlet, Invoke-RestMethod, for interacting with REST-based web services. This cmdlet is used to send HTTP and HTTPS requests to REST-based web services that return structured data. PowerShell will then take the response and format it based on the data type. Visual Studio Online returns JSON data, which PowerShell will convert (or deserialize) into objects.

Take a look at the following script, which is used to return the details of a specific work item from Visual Studio Online:

First, I configure the username and password. Notice this is where I specify the alternate authentication credentials that I created earlier in the post. Next, I configure the basic authentication header.  Finally, I call the Invoke-RestMethod cmdlet.  I pass in the URI for accessing a specific work item, as defined in the API documentation.  I also specify my authentication header, and the method I’m using, in this case Get.  Here is the output from running this script.

image

As you can see, it returned detailed information about the work item, including the work item id, how many times it has been updated, and detailed field information.

We are just getting started. For the next few posts, I’m going to dive into the REST API, and show how I can manipulate different features, such as build, work items, and version control, using PowerShell.

One thought on “Day 58: PowerShell and Visual Studio Online

  1. Pingback: Build Scripts | Vincent

Leave a Reply