在Azure DevOps发布管道中使用Az模块

Col*_*kay 7 azure-powershell azure-devops

我想在Azure DevOps发布管道中使用Azure PowerShell模块(又名Az模块)。但是,现有的PowerShell选项似乎无效。我已经尝试了各种方法,但是安装新的Az模块只会带来很多错误,包括Az and AzureRM modules cannot be imported in the same session or used in the same script or runbook.

常规的PowerShell任务没有内置的任何Azure模块。AzurePowerShell任务使用已淘汰的Azure RM PowerShell模块(即,它仍受支持,但不会添加任何新功能)。

上面的错误很可能是由于Azure PowerShell任务启动时在运行我的脚本之前执行以下操作引起的:

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' 
Run Code Online (Sandbox Code Playgroud)

自然,在一个新项目中,我不想使用不再高级的模块来创建PowerShell脚本。

再想一想,即使我确实设法克服了这一点,我也必须以某种方式对Azure进行身份验证,现有的Azure DevOps UI会为我进行身份验证,而我还看不到如何使用Az模块来做到这一点。 将订阅添加到Azure上下文

谷歌搜索似乎无济于事,因为大多数信息仍与已弃用的AzureRM模块相关。所以...总而言之。

如何在Azure DevOps发布管道中使用Azure(Az)Powershell模块?

Div*_*hta 5

尝试使用 Azure Powershell 任务版本 4.*(预览版)。此功能仍处于预览阶段。将此与自托管代理一起使用。这将很快针对 Microsoft 托管代理发布。

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

在此输入图像描述


Dan*_*ann 1

该任务依赖于特定模块。如果您想使用该任务,除了等待他们更新之外,您无法执行任何操作。

如果您愿意,您可以分叉任务存储库并自行更新。或者您可以编写自己的 Azure 身份验证逻辑。

您可以使用反向别名 (Az -> AzureRM) 编写脚本,而不是使用 Az 模块提供的别名 (AzureRM -> Az),因此您可以使用将支持的 cmdlet 名称编写脚本,因此将来 -证明自己。