Working with TFS and PowerShell (Examples)

choubertsprojects

The Best WordPress plugins!

1. WP Reset

2. WP 301 Redirects

3. WP Force SSL

TFS is a highly customizable and powerful Microsoft-based workflow platform. This tutorial will focus on using PowerShell with TFS to automate workflows, run scripts, and provide solutions for automating large tasks that may otherwise be unachievable without scripting.

The “tfs power tools” is a set of PowerShell modules that allow you to automate various tasks in Team Foundation Server. This article will go into detail about the different cmdlets and how they can be used.

Working with TFS and PowerShell (Examples)

This article is for you if you’re using Microsoft’s Team Foundation Server (TFS) and need to administer it using PowerShell. In this article, you’ll learn how to manage TFS using TFS PowerShell examples. You never know, you could wish to use this information to create a TFS PowerShell module!

TFS has a robust API that can be used with PowerShell to provide a convenient method to control TFS from the command line and through PowerShell scripts.

In this tutorial, we’ll go through the fundamentals of using PowerShell to connect to TFS for the first time. The code you’ll be using may then be utilized to investigate other PowerShell API actions and construct more complex features.

Creating a TFS account

You must first authenticate before using the TFS REST API. You may utilize a PSCredential object in PowerShell to do this. Run Get-Credential and supply an account with TFS access to build a PSCredential object.

All Things Credentials and Using the PowerShell Get-Credential Cmdlet

After you’ve stored a credential as a variable, you may use it in many calls to the PowerShell cmdlet Invoke-RestMethod.

The PowerShell cmdlet Invoke-RestMethod enables us to easily create and submit multiple HTTP requests to REST APIs. I’ll build a splatted parameter set with Credential inside so I don’t have to supply it every time I run Invoke-RestMethod.

@ Credential = $credential $invRestMethParams = @

Using TFS PowerShell as an Example

To make sure you don’t get an error, submit a test query to TFS. Attempt to obtain all of the projects on the server that you have access to. This may be accomplished by using the HTTP GET technique to communicate with the API. But first, you’ll need to figure out what the URI is. The URL pattern for retrieving all of the projects will be as follows:

http(s)://<TFSServer>/<TeamCollection>/<TeamProject>/_apis/projects?api-version=2.0

As you can see, I’m utilizing the API version 2.0. This is what my URI looks like:

$uri = ‘http://tfs.domain.local:8080/tfs/IT/ apis/projects?api-version=2.0’ $uri = ‘http://tfs.domain.local:8080/tfs/IT/ apis/projects?api-version=2.0’

Now that you have the URI, you can use it in the Invoke-RestMethod arguments.

PS51> $invRestMethParams.Uri = $uri

The additional arguments will need to be added to Invoke-RestMethod next.

$invRestMethParams. ‘Get’ is the method. $invRestMethParams. ‘application/json’ as the ContentType ## My final arguments, as well as the Invoke-RestMethod call, will be as follows: @invRestMethParams = $invRestMethParams $credential $uri $uri $uri $uri $uri $uri $uri $uri $uri $uri $uri $uri $uri $uri $uri $uri $uri $uri $uri $uri $uri ‘application/json’ as the content type @invRestMethParams Invoke-RestMethod

When I execute the following code, I get something like this as a result. Invoke-RestMethod is used in this TFS PowerShell example to make an API call.

Using PowerShell to locate TFS projectsUsing PowerShell to locate TFS projects

This isn’t really useful. You must examine the value property’s contents to find what you’re searching for. When you look at the value property, you’ll see that each of the projects in the collection will appear.

Getting to the bottom of TFS projectsGetting to the bottom of TFS projects

This indicates that you’ve successfully authenticated with TFS and requested the right URI.

The world is your oyster at this stage. You may want to view all of the builds that are available to you. I just need to alter the Uri parameter since I’ve saved all of the parameters within the hashtable.

$invRestMethParams.Uri = ‘http://tfs.domain.local:8080/tfs/IT/IS-DevOps/ apis/build/builds?api-version=2.0’ $invRestMethParams.Uri = ‘http://tfs.domain.local:8080/tfs/IT/IS-DevOps/ apis/build/builds?api

Using PowerShell to locate TFS buildsUsing PowerShell to locate TFS builds

It’s clear that after you’ve been authorized and figured out the right URL scheme, all you have to do now is change the URL to accomplish what you want. Check out the Visual Studio getting started guide to learn a lot more about what you can accomplish with the TFS REST API.

Steps to Follow

You can start controlling and automating many things with TFS using PowerShell. Using PowerShell to manage TFS enables you to include multiple TFS procedures into ideas such as a CI/CD pipeline.

Now see if you can improve on what you’ve learnt by writing a TFS PowerShell module!

Understanding and Creating PowerShell Modules is a related topic.

The “tf. command in powershell” is a way to use TFS and PowerShell together. This is a simple example of how it can be done.

Related Tags

  • tfscmdlets
  • powershell scripting for devops
  • connect to tfs from command line
  • azure devops powershell script arguments
  • powershell tf get

Table of Content