Magic with Azure DevOps PowerShell Module
Tags: azure, azuredevops, powershell
Published: 2020-11-12 08:51:31

####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](https://dev.to/vivekanandrapaka/terraform-and-azure-devops-120p) 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](https://developer.microsoft.com/en-us/advocates/donovan-brown) (Principal DevOps manager at Microsoft) and [Sebastian Schütze](https://www.razorspoint.com/author/razor/) (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](https://www.donovanbrown.com/post/PowerShell-I-would-like-you-to-meet-TFS-and-VSTS)

[AzureDevOps module on Powershell gallery](https://www.powershellgallery.com/packages/VSTeam/7.1.2)

[AzureDevOps module in Choclatey](https://chocolatey.org/packages/microsoft-vsteam-psmodule)

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](https://dev-to-uploads.s3.amazonaws.com/i/4lm97ji2vtzqpd7t2hm1.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/1gw3igd12bwm5uk7uh08.png)

3.Copy the token and secure it. You need to use it later.
>READ the warning message

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ai3bfm5kaptsirau1551.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/lg3su8nt4mhlb6i7jl8n.png)

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/o4idja3qy48dds19y3p0.png)

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/w9s2hc24stlz5rxruvd7.png)


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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lramvhwhowa7xqm19j8b.png)


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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ifi2mniby4y8ln14tumf.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/2kh2nhahruabb9i2eya0.png)

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/svb9uogtqq4fxeqyg49m.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/600b95hsaygbdi4xz6zm.png)

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/x8yozouy7mym9rph08t9.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/232iux4mwoqbkbu7k7lz.png)

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/alf57r53ypmscqm500v7.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/gnitb1587h7mjq54riby.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/ur0y5prxbbgrlt2iz854.png)

Also, Here is the GitHub [link] (https://github.com/MethodsAndPractices/vsteam) for the module and it has detailed documentation on how can you can contribute to it.

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