8
votes

I want to use the Azure PowerShell Module (aka Az Module) in an Azure DevOps Release Pipeline. However, the existing PowerShell options don't seem to work. I've tried various things, but installing the new Az module just brings up a ton of errors, including Az and AzureRM modules cannot be imported in the same session or used in the same script or runbook.

The regular PowerShell task doesn't have any azure modules built in. And the Azure PowerShell task uses the Azure RM PowerShell module, which has been retired (i.e. it is still supported, but no new features will be added).

The error, above, is most likely caused because when the Azure PowerShell task start, it performs the following before running my script:

2019-01-13T13:34:14.5416432Z ==============================================================================
2019-01-13T13:34:14.5416555Z Task         : Azure PowerShell
2019-01-13T13:34:14.5416623Z Description  : Run a PowerShell script within an Azure environment
2019-01-13T13:34:14.5416705Z Version      : 3.1.18
2019-01-13T13:34:14.5416762Z Author       : Microsoft Corporation
2019-01-13T13:34:14.5416831Z Help         : [More Information](https://go.microsoft.com/fwlink/?LinkID=613749)
2019-01-13T13:34:14.5416969Z ==============================================================================
2019-01-13T13:34:20.3546127Z ##[command]Import-Module -Name C:\Modules\AzureRm_6.7.0\AzureRM\6.7.0\AzureRM.psd1 -Global
2019-01-13T13:34:58.4365259Z ##[command]Clear-AzureRmContext -Scope Process
2019-01-13T13:34:59.2732327Z ##[command]Disable-AzureRmContextAutosave -ErrorAction SilentlyContinue
2019-01-13T13:35:00.1691359Z ##[command]Add-AzureRMAccount -ServicePrincipal -Tenant *** -Credential System.Management.Automation.PSCredential -Environment AzureCloud @processScope
2019-01-13T13:35:01.5702545Z ##[command] Select-AzureRMSubscription -SubscriptionId d5eaaba3-2968-456a-98a4-e53e961fc896 -TenantId ***
2019-01-13T13:35:02.1592660Z ##[command]& 'D:\a\r1\a\ws-build\tools\install-dependencies.ps1' 

Naturally, on a new project I don't want to be creating PowerShell scripts with a module that will no longer be advanced.

And thinking ahead, even if I do manage to get past this, I will have to authenticate somehow with Azure, which the existing Azure DevOps UI does for me, and I can't yet see how to do that with the Az Module. Add a subscription into the Azure context

Googling doesn't seem to help, as most information still relates the deprecated AzureRM module. So... In summary.

How do I use the Azure (Az) Powershell module in an Azure DevOps release pipeline?

5
This is coming soon. According to this GitHub comment, the Az modules will be available in Feb, 2019: github.com/Microsoft/azure-pipelines-tasks/issues/…Colin Mackay
I tried playing with using Az modules in Azure devops. Have written a brief about it in here - medium.com/@harioverhere/…Hari Subramaniam

5 Answers

4
votes

Try using Azure Powershell Task version 4.* (Preview). This feature is still in preview. Use this with Self Hosted agents. This will be released for Microsoft Hosted agent soon .

Reference - https://developercommunity.visualstudio.com/content/problem/434058/unable-to-use-new-az-module-in-azure-devops.html

enter image description here

3
votes

As a workaround I have been using the Hosted VS2017 Agent Pool as these have got the Az module ready to use

1
votes

The task has a dependency on a specific module. If you want to use the task, you can't do anything about it other than wait for them to update it.

If you'd like, you can fork the task repository and update it yourself. Or you can write your own logic for Azure authentication.

You could write your scripts with a reverse alias (Az -> AzureRM) as opposed to the aliases provided by the Az module (AzureRM -> Az), so you can write your scripts using the cmdlet names that will be supported, thus future-proofing yourself.

1
votes

I took the hack route, but I ended up creating a dummy AzureRM module to install on my self-hosted agents, and then modified the scripts for the Azure PowerShell task on my agents to load the AzureRM aliases. The end result is that I have Az installed on my agents and they can run AzureRM style cmdlet names and/or Az style cmdlet names on either version 3 or 4 of the Azure PowerShell task. This allowed me to switch to Az on the build agents and allow teams to gradually migrate their scripts to use the Az cmdlet names.

My solution is on Github: https://github.com/brendonthiede/dummy-AzureRM

One caveat is that this may require "remodifying" the Azure PowerShell scripts on the agents every time the task is updated.

0
votes

Not each version of Azure Powershell Task has both Az and AzureRm modules

enter image description here