Azure DevOps PowerShell 任务无法加载 'Az.CosmosDB' 模块

Azure DevOps PowerShell task fails to load 'Az.CosmosDB' module

我有一个 Azure DevOps 管道来轮换 Cosmos DB 帐户密钥。为此,我使用了 PowerShell 和 New-AzCosmosDBAccountKey cmdlet。

由于某些未知原因,Az.CosmosDB 模块未与 Az 一起安装,因此每次管道 运行 时都需要手动安装它。

Install-Module -Name Az.CosmosDb -AllowClobber -Force

当我在本地 运行 时,一切都按预期工作,但在 Azure DevOps 中我看到一个错误

The 'Get-AzCosmosDBAccountKey' command was found in the module 'Az.CosmosDB', but the module could not be loaded.

Azure DevOps 中可能发生的事情与本地发生的事情不同?

请检查运行 DevOps Pipeline 时,您使用的是哪个代理池。如果你使用 microsoft-hosted 代理,每次你 运行 一个管道,你都会得到一个新的虚拟机。虚拟机使用一次后即被丢弃。所以每次都需要手动安装Az.CosmosDB模块

你也可以在你的本地机器上部署一个代理,这样,应该不需要每次都手动安装Az.CosmosDB模块。

https://docs.microsoft.com/en-us/azure/devops/pipelines/agents/agents?view=azure-devops&tabs=browser

这里的答案是对 Az.CosmosDb 模块进行了更新,因此它特别需要 Az.Accounts 的 1.9.4 版(或更高版本)。但是,在 Azure DevOps 托管代理上,他们使用版本 1.9.3。

为了修复,我更改了手动安装 Az.CosmosDb 模块的命令 -

Install-Module -Name Az.CosmosDb -RequiredVersion 0.1.6 -AllowClobber -Force

备注

当我在 PowerShell Gallery 上发现 Az.CosmosDb 模块的所有版本都是 < 1.0.0 时,我做了一些调查,结果发现该模块是尽管 Cosmos DB 已经存在多年,但仍处于预览阶段。这解释了为什么在安装 Az.

时该模块没有与所有其他 Az.* 模块一起安装

非常令人沮丧的是 documentation 中没有任何地方提到该模块处于预览状态。如果有的话,它可能会节省我很多时间!

看来今天 Azure DevOps 终于打破了一切,旧变体

Install-Module Az.CosmosDB -RequiredVersion 0.1.6 -Force

已停止工作。太棒了,Az DevOps 和 Azure 技术的整体体验非常相似,每次它们只是突然在随机位置停止工作!

更新:图库源地址已失效https://www.powershellgallery.com/packages/Az.CosmosDB/0.1.6,看来是这个原因