DEV Community

Cover image for Magic with Azure DevOps PowerShell Module
Vivekanand Rapaka
Vivekanand Rapaka

Posted on • Edited on

Magic with Azure DevOps PowerShell Module

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!!!

Top comments (8)

Collapse
 
kishoredevops profile image
kishoreoptum

Hi Vivek,

I'm working on VSTeams powershell, Could you help me here, how can I set the apiversion of graph as I'm seeing 404 error when I try to get the users using Get-VSTeamUser cmdlet.

Suggest me any other way to get users and Groups from Azure Devops?

Thanks,
Kishore

Collapse
 
vivekanandrapaka profile image
Vivekanand Rapaka • Edited

Hello Kishore,

I'm able to run 'Get-VSTeamUser cmdlet and get the output with the list of users. By using below command.

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

and then run 'Get-VSTeamuser' cmdlet. I made sure that the generated PAT has full access to the ADO account

dev-to-uploads.s3.amazonaws.com/up...

Collapse
 
kishoredevops profile image
kishoreoptum • Edited

Hi Vivek,

Thanks for your reply!!

Actually I got some Admin access but still seeing error and its attached, this means I don't have full access in AzureDevOps, could you please confirm?

If I set apiVersion using Set-VSTeamAPIVersion cmdlet then seeing different message that's also I've attached. Could you please go through it and suggest me.

Screen shot1: dev-to-uploads.s3.amazonaws.com/up...
Screen shot2: dev-to-uploads.s3.amazonaws.com/up...

Thanks,
Kishore

Thread Thread
 
vivekanandrapaka profile image
Vivekanand Rapaka

I ran "help Set-VSTeamAPIVersion" to see the list of accepted parameters for Set-VSTeamAPIVersion, found that it only accepts following values. Looks like 'graph' is not supported.

-Service
Specifies the service to change. The acceptable values for this parameter are:

    - Build

    - Release

    - Core

    - Git

    - DistributedTask

    - Tfvc

    - Packaging

    - MemberEntitlementManagement

    - ExtensionsManagement

    - ServiceEndpoints

    Required?                    true
    Position?                    named
    Default value                None
    Accept pipeline input?       false
    Accept wildcard characters?  false
Enter fullscreen mode Exit fullscreen mode

dev-to-uploads.s3.amazonaws.com/up...

dev-to-uploads.s3.amazonaws.com/up...

Thread Thread
 
kishoredevops profile image
kishoreoptum • Edited

Just checked help and Graph is not available.

But before I've followed git source code and I tried to work on the set apiversion. Please check code from below link that says graph is supported. Am I missing anything here.

github.com/MethodsAndPractices/vst...

Thanks
Kishore.

Thread Thread
 
vivekanandrapaka profile image
Vivekanand Rapaka

Please check with the maintainers, but looks like its not supported.

Thread Thread
 
kishoredevops profile image
kishoreoptum

Ok.. sure, thank you

Collapse
 
ayanmullick profile image
Ayan Mullick

Thanks for the post. How does one run an ADO query using the module?
github.com/MethodsAndPractices/vst...