To show you how to use AzureDevOps PowerShell module to achieve some basic tasks that you can do using PowerShell.
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.
Here are a couple of prerequisites you need if you would like to replicate below in your environment
- PowerShell (Obviously) - Its suggested that you have latest and greatest version of PowerShell, but any PowerShell version after 5.1 works just fine.
- AzureDevOps PowerShell module – I’ll show you how to install this in this blog post.
- 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 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
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
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.
3.Copy the token and secure it. You need to use it later.
READ the warning message
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)
You see that I already had the module, I ran with ‘-force’ parameter to upgrade it.
5.Assign the Personal access token generated in the previous step to a variable for easy reuse.
6.Type following commands to set up your account:
Set-VSTeamAccount -Account https:// https://dev.azure.com/yourorganizationname/ -PersonalAccessToken $PAT
7.Let’s list the projects in the Azure DevOps.
Get-VSTeamProject # Lists all the available Projects.
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
Get-VSTeamReleaseDefinition -ProjectName $Project
I have one CD release pipeline, that its showing in the above output.
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
Great, we can see the variables in output. Now let’s see the variables from release pipeline to verify.
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.
We have an environment Property. Let’s expand on that.
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
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.
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!!!