PowerShell in Azure DevOps – Chapter 3

If you are new to PowerShell, then my earlier chapters will help you scale on this topic quickly – chapter 1 – introduces PowerShell and chapter 2- writing scripts in PowerShell. The scripts that you create and execute are running on the local machine, what if we need to run these scripts in a pipeline in Azure?

Azure DevOps is a SaaS (Software as a Service) based platform from Microsoft that helps-

a. enables architects and leads to define workflows, release trains and design self-service catalogs,

b. developers implement and orchestrate CI (Continuous Integration) /CD (Continuous Delivery), and

c. empowers infrastructure leads to implement the IaaC (Infrastructure as a Code) using ARM (Azure Resource Manager) templates with PowerShell.

Azure DevOps- Key Features

Based on your role in the organization, when you log into the Azure platform (typically the URL looks like this – https://dev.azure.com/<<your org structure name>>/), you could see the list of projects against which you have access. Select the project that allows you to create pipelines. Let us assume that we have an organization with the name “My Org” against which we have access. When we login into Azure, it shows us the Org name as well as the projects within it. Below is a quick sample snapshot-

Logging into Azure DevOps

So, “My Org” is the organization and it comprises of one project called “AzureProject”. Click on this project to view the contents inside it.

View Azure Project

The left panel of this window shows various functionalities that are available, in simple terms it shows the features against which you have permissions. The absence of any feature in this panel, indicates limited access. Now, this with basic background, lets get started with the steps of introducing PowerShell in an existing Azure pipeline.

Step 1 –> Click on the “Pipelines” option in the left panel and then select an existing pipeline, in our example, we will select a pipeline called “TestPipeline” –

View Pipelines

On clicking the pipeline name, another window pops up that displays the recent build status of the pipeline. Click on the EDIT button on the top right corner-

Editing Existing Pipeline

Step 2 –> This is the window where you customize your pipeline, in simple terms you orchestrate the different tasks or jobs in the pipeline. For our example, we will click on the “Windows Agent” option to insert PowerShell script-

Insert Windows Agent

Type in “powershell” in the search textbox on the right panel and you will be able to view a number of ways to work with PowerShell-

Inserting PowerShell as a Task in Azure DevOps

Azure DevOps provides a number of options to work with PowerShell – either as a simple script or as a service fabric, it also offers its own version called Azure PowerShell. For our example, we will select the option “PowerShell” and click on “Add” button.

Step 3 –> Mention a name to this step/task – “MyFirstTestScript” and select whether an existing PowerShell is to be called or you will type in the script in this step itself-

Options to work with PowerShell

We will select “Inline” option to type in the commands in this task.

Writing PowerShell as Inline Script in Azure DevOps

Step 4 –> Now save this pipeline and queue it for execution-

Running Task

Step 5 –> The pipeline will prompt asking for running it now or later, click on Save and Run option. As the pipeline runs, it generates a build number, click on the “Windows Agent” option to view results of the pipeline-

View Pipeline

The queue results shows the tasks performed in the pipeline. Each job/task is monitored. Click on your task and view the results in the right window-

View Build Status and Results

You can see that the PowerShell cmdlet that we had created in the task gets displayed in the output window-

Final ouput

You can go back to the pipeline, customize and add more cmdlets in your script to test and see the results. Isn’t that easy? 😊

Try out by adding more tasks and calling existing PowerShell scripts. Also to note is that, the tasks in the pipeline can be orchestrated in a way that output of one task becomes an input for another task. In the next chapter, we will explore on APIs in PowerShell.

Quick References: