Get-AzTrafficManagerProfile 命令未从管道内部运行

问题描述

我需要更新管道中的流量管理器配置文件以切换资源,当我在我登录的本地 az cli 中运行命令 Get-AzTrafficManagerProfile 时,它​​运行没有问题, 但是当我将它作为 Azure 管道的任务部分运行时,它抛出了以下错误

Get-AzTrafficManagerProfile :在上下文中找不到帐户。请使用 Connect-AzAccount 登录

还尝试从管道内部调用 Connect-AzAccount,但没有成功。

解决方法

要在 devops 中成功运行命令 Get-AzTrafficManagerProfile,您需要使用 Azure PowerShell task 并使用 Azure Resource Manager service connection 进行身份验证。

请参考以下步骤。

1.创建一个 Azure PowerShell task,在 Azure subscription -> Available Azure subscriptions -> 选择您要运行 Get-AzTrafficManagerProfile 的订阅。

enter image description here

2.然后点击Authorize按钮,它会自动为您创建一个Azure Resource Manager service connection并在订阅范围内分配一个Contributor RBAC role

注意:请确保您登录 devops 的用户帐户是订阅中的 Owner/User Access administrator,否则您无权成功执行此操作。

enter image description here

3.然后在Inline Script中直接使用Get-AzTrafficManagerProfile就可以了,不需要手动登录,因为它会自动通过服务连接登录。

enter image description here

4.测试任务,它工作正常。

enter image description here