启用 Azure DevOps 管道以将角色分配给 Azure 资源

问题描述 投票:0回答:1

我需要执行一个 PowerShell 脚本,该脚本与其他内容一起将角色分配给 Azure 自动化帐户以成为数据工厂贡献者

#Configure Access to data factory:
$accountObj = Get-AzAutomationAccount -Name "$objectName" -ResourceGroupName 
"$resourceGroupName"
$identityId = ($accountObj).Identity.PrincipalId
$scope="/subscriptions/<subscrId>/resourceGroups/resGrp/
   providers/Microsoft.DataFactory/factories/ADF"

根据 https://learn.microsoft.com/en-us/azure/role-based-access-control/built-in-roles#user-access-administrator

为资源分配角色。 (在我的例子中允许自动化帐户成为数据工厂贡献者)

当我从 Azure 门户 - Posershell 云以我自己(所有者)身份运行该脚本时,该脚本工作正常

但是,我需要它从 Azure DevOps 管道运行,并且我已经拥有一组通过运行 Powershell 脚本创建各种资源的管道。 我在调用命令的阶段遇到错误。 - 创建自动化帐户和导入 Runbook 的任务工作正常。

我知道我的 Azure Pipeline 需要被允许分配角色,但是..我不知道如何为其添加权限。

我看这里

https://learn.microsoft.com/en-us/azure/role-based-access-control/built-in-roles#user-access-administrator

但是,我没有那个

“roleName”:“用户访问管理员”,搜索时分配给开发运营管道。

到目前为止,我的 ADO 管道扮演着“贡献者”的角色。这样我就可以创建 Azure 数据工厂、具体的 Key Vault 和其中的机密。 ..以及创建自动化帐户及其运行手册。

这就是我迷失的地方......我在寻找什么?.. 从调试输出来看,失败的行是对

的调用
New-AzRoleAssignment -ObjectId $identityId -RoleDefinitionName ($role).Name -Scope $scope
azure azure-devops rbac
1个回答
0
投票

如果您的 DevOps 服务主体不具有分配角色所需的用户访问管理员角色,通常会发生此错误。

当我在具有

Contributor
角色的 DevOps 管道中运行以下脚本时,我也遇到了同样的错误,如下所示:

$accountObj = Get-AzAutomationAccount -Name "sriautomationacc2609" -ResourceGroupName "rgname"
$identityId = ($accountObj).Identity.PrincipalId$scope="/subscriptions/subId/resourceGroups/rgname/providers/Microsoft.DataFactory/factories/ADF"
$roleName = "Data Factory Contributor"
New-AzRoleAssignment -ObjectId $identityId -RoleDefinitionName $roleName -Scope $scope

回复:

enter image description here

要解决该错误,请访问

Service connections
选项卡并单击 管理服务主体 选项,如下所示:

enter image description here

它会打开 Azure 门户,其中包含您的 DevOps 服务主体 页面,您可以在其中找到服务主体名称:

enter image description here

现在,将“用户访问管理员”角色分配给订阅下的 Azure DevOps 服务主体,如下所示。

enter image description here当我在分配

User Access Administrator

 角色后再次运行 DevOps 管道时,我成功获得了 
response,如下所示: $accountObj = Get-AzAutomationAccount -Name "sriautomationacc2609" -ResourceGroupName "rgname" $identityId = ($accountObj).Identity.PrincipalId$scope="/subscriptions/subId/resourceGroups/rgname/providers/Microsoft.DataFactory/factories/ADF" $roleName = "Data Factory Contributor" New-AzRoleAssignment -ObjectId $identityId -RoleDefinitionName $roleName -Scope $scope

回复:

enter image description here由于您已经拥有

Owner

角色,您还可以在 Azure Cloud Shell 中运行以下 PowerShell 命令将角色分配给 DevOps 服务主体: (Get-AzADServicePrincipal -DisplayName <DevOpsAppname>).id New-AzRoleAssignment -ObjectId <DevOpSpId> -RoleDefinitionName "User Access Administrator" -Scope "/subscriptions/subId/"

回复:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.