DEV Community

loading...
Cover image for Magic with Azure DevOps PowerShell Module

Magic with Azure DevOps PowerShell Module

vivekanandrapaka profile image Vivekanand Rapaka Updated on ・5 min read

Purpose of this blog post

To show you how to use AzureDevOps PowerShell module to achieve some basic tasks that you can do using PowerShell.

Assumptions

I assume that you have fair knowledge on how to use PowerShell for day-to-day operations and some practical working experience on Azure DevOps.

Prerequisites needed

Here are a couple of prerequisites you need if you would like to replicate below in your environment

  1. PowerShell (Obviously) - Its suggested that you have latest and greatest version of PowerShell, but any PowerShell version after 5.1 works just fine.
  2. AzureDevOps PowerShell module – I’ll show you how to install this in this blog post.
  3. Azure Devops Account - With few build & release pipelines setup. check out one of my previous blog posts on how to setup an end to end Azure DevOps Pipeline

AzureDevOps PowerShell Module

AzureDevOps module is open-source PowerShell module, first released in the year 2017. It is owned and maintained by Donovan Brown (Principal DevOps manager at Microsoft) and Sebastian Schütze (Azure Nerd with focus on DevOps and AzureDevOps). and there have been many new versions to it. The current version as of this article is 7.1.2.

If you are an infrastructure engineer or developer, I’m sure that you have used PowerShell in past and the capabilities that you get with PowerShell are simply awesome. Other than the native cmdlets available with default in-built modules available in PowerShell, there are lot of community-driven/open-source modules available for achieving desired actions.

Once such module is AzureDevOps PowerShell module.

Just like any other PowerShell module for respective functions, you can use the cmdlets in this module to interact with REST API of Azure DevOps for all the aspects of Azure DevOps. We already have AzureDevOps CLI offered by Microsoft and it does the same.

If you are a PowerShell fan like me, you would look for a module that offers cmdlets for respective functions for any of the technologies.

To know more about Azure DevOps Powershell Module, please visit following link

AzureDevOps module on Powershell gallery

AzureDevOps module in Choclatey

Ok, Let’s get started.

First, we’ll take a look at how to install AzureDevOps module and the capabilities this module offers. It’s pretty straight forward.

We need a PAT (personal access token) from Azure DevOps to consume it from the cli. So, we’ll generate that first and then install the PowerShell module.

1.Login to your Azure DevOps organization and generate personal access token for your account

Alt Text

2.Select “New Token” and give it a name, choose full access.

NOTE: Do not share this token with anyone. Anyone who has this token has access to your AzureDevOps account.

Alt Text

3.Copy the token and secure it. You need to use it later.

READ the warning message

Alt Text

4.Run PowerShell as administrator and run following command:
Install-Module -Name VSTeam
(run it with –force parameter to upgrade to latest version if the module is already installed)

Alt Text

You see that I already had the module, I ran with ‘-force’ parameter to upgrade it.

Alt Text

5.Assign the Personal access token generated in the previous step to a variable for easy reuse.
$PAT="yourtokengoeshere"

Alt Text

6.Type following commands to set up your account:

Import-Module VSTeam
Set-VSTeamAccount -Account https:// https://dev.azure.com/yourorganizationname/ -PersonalAccessToken $PAT

Alt Text

7.Let’s list the projects in the Azure DevOps.
Get-VSTeamProject # Lists all the available Projects.

Alt Text

8.Let’s set a project to a $project to a variable and use below cmdlet for exploring the build definitions, etc.

you can use any of your projects from previous output

$Project='Terraform'

Get-VSTeamReleaseDefinition -ProjectName $Project

Alt Text

I have one CD release pipeline, that its showing in the above output.

Alt Text

9.Let’s explore a little more. How about exporting the variables for this release definition?

Type the following command to get the variables.

Get-VSTeamReleaseDefinition -ProjectName $project -Id 1 | Select -expand Variables | Format-List

Alt Text

Great, we can see the variables in output. Now let’s see the variables from release pipeline to verify.

Alt Text

Looks like we received only the release-scoped variables alone.

Let’s see on how to obtain values for scope for ‘Dev’

Let’s pipe our previous cmdlet to Get-member to see if there exists a property for environments.

Alt Text

We have an environment Property. Let’s expand on that.

Alt Text

In the above output, we can see the variables which are scoped to ‘Dev’

10.Now, let’s run following command to get environment details for respective environments

Get-VSTeamReleaseDefinition -ProjectName $project -Id 1 | Select -expand environments | Where name -like 'DEV' | Select -Expand Variables | Format-List

Alt Text

In the above output we can see the variables scoped for ‘Dev’ environment.

Similarly, if you have additional stages, you can provide the environment name in the above command to retrieve the values. This is especially helpful when you want to extract the variables and store it for future reference.

This brings us to the end of this blog post.

There are many other cmdlets that we can use to play around and get a lot done with help of this module. We can write scripts or a simple one liner to trigger the releases, create reports based on the release run, etc.

I highly encourage you to go through the various other cmdlets available for use by typing Get-Command.

Alt Text

Also, Here is the GitHub link for the module and it has detailed documentation on how can you can contribute to it.

Thanks for reading this blog post, happy learning!!!

Discussion (0)

pic
Editor guide