【问题标题】:Azure CLI Equivalent to Powershell's Add-MsolRoleMemberAzure CLI 等效于 Powershell 的 Add-MsolRoleMember
【发布时间】:2017-05-22 22:21:55
【问题描述】:

我正在使用Azure CLI 2.0 并试图找到与以下 Powershell 命令等效的命令:

Add-MsolRoleMember -RoleObjectID $roleID -RoleMemberType ServicePrincipal -RoleMemberObjectId $appObjectId

我正在使用此命令将 Azure AD 应用程序主体添加到 “用户帐户管理员” 角色。

我使用此链接安装了MSOL Powershell libraries,其中包括“Add-MsolRoleMember”命令。

因为我需要在 Mac、Linux 等平台上运行的命令,我正在尝试使用最新版本的“Azure CLI”,尽管我对其他代码示例(例如 Python)持开放态度。

以下是我目前尝试过的 cli 命令:

az login --tenant <my tenant id>
# I get my app id from this:
az ad app list
# I cannot get the following to work, and suspect I am running 
# the incorrect command
az role assignment create --assignee <my app id>  --role <role id>

所以我的问题是:如何使用 CLI 或其他在 Mac、Linux 等上运行的库向应用主体添加角色?

【问题讨论】:

    标签: python powershell azure-active-directory azure-cli azure-cli2


    【解决方案1】:

    不幸的是,据我所知,Linux 上没有库工作我们可以将成员添加到 Azure 组织角色。

    目前,我们可以使用 Azure CLI 2.0 来管理 Azure AD userappgroupservice principal,但我们不能使用 CLI 2.0 将成员添加到 azure 组织角色。

    az role assignment create --assignee <my app id>  --role <role id>
    

    此命令az role assignment 用于管理资源组角色。例如,我们要授予用户 Owner 权限。我们可以在此屏幕截图中通过 Azure 门户找到此角色:

    关于组织角色,我们可以在这个官方article找到他们,例如Billing AdministratorCompliance Administrator。与资源组权限不同。

    您可以向此link 提供反馈,您在此链接中分享的所有反馈都将由 Microsoft 工程团队监控和审核。

    ============
    更新:

    【讨论】:

    • 我有多个 Azure AD 实例和多个资源组。我在我的任何资源组的 IAM 部分都找不到“用户帐户管理员”。你能提供说明吗?我也尝试过 Azure AD 下的“目录角色”部分,但这并没有显示“用户帐户管理员”,如本文所示:docs.microsoft.com/en-us/azure/active-directory/…
    • @GregThatcher 对不起,我没有描述清楚,我的意思是有两个角色,一个是目录角色,另一个是资源组角色,有不同,现在我们可以使用 CLI 2.0 来设置资源组角色。目前,User account Administrator 已更改,新名称为 User administrator。我会将这个截图添加到我的答案中,请检查一下。
    • 感谢@JasonYeMsft,但我相信截图是为普通用户添加角色。我需要将此角色添加到与我的一个 Azure AD 实例中的“应用程序注册”关联的应用程序主体(也称为“服务主体”)。新的 Add-AzureADDirectoryRoleMember 和 Get-AzureADDirectoryRole Powershell 命令看起来很有前途(并且可能是跨平台的),但我无法让它们工作。
    • 根据你的描述,可能你正在寻找这个命令,Get-AzureADApplicationSet-AzureADApplication -AppRoles,更多关于应用角色的信息,请参考这个链接docs.microsoft.com/en-us/azure/active-directory/develop/…
    • @GregThatcher 你试过这个命令set-AzureADapplication -Approles 吗?
    【解决方案2】:

    如何做到这一点的好文章可以在这里找到:https://blogs.msdn.microsoft.com/aaddevsup/2018/08/29/how-to-add-an-azure-ad-role-to-a-enterprise-application-service-principal/

    代码如下:

    # Fetch User Account Administrator role instance
    $role = Get-AzureADDirectoryRole | Where-Object {$_.displayName -eq 'User Account Administrator'}
    
    # If role instance does not exist, instantiate it based on the role template
    if ($role -eq $null) {
        # Instantiate an instance of the role template
        $roleTemplate = Get-AzureADDirectoryRoleTemplate | Where-Object {$_.displayName -eq 'User Account Administrator'}
        Enable-AzureADDirectoryRole -RoleTemplateId $roleTemplate.ObjectId
    
        # Fetch User Account Administrator role instance again
        $role = Get-AzureADDirectoryRole | Where-Object {$_.displayName -eq 'User Account Administrator'}
    }
    
    #Now that we have the object IDs for the AAD role, we will need to get both object IDs to add the role to the enterprise application. We can use the command below :
    Add-AzureADDirectoryRoleMember -ObjectId $role.ObjectId -RefObjectId $BeckeChB2Cs0v1GraphApiAdSPN.ObjectId
    

    【讨论】:

    • 请求是提供 Azure CLI 等效项,以便它可以在 Linux/macOS 上完成,而不是只能在 Windows 上运行的 PowerShell 代码。并且您提供 PowerShell 代码。
    【解决方案3】:

    我相信这可以通过 Microsoft.Graph 模块中的 Update-MgRoleManagementDirectoyRoleAssignment 命令在跨平台的 Powershell 7 上完成。

    来自 Powershell 7:

    Install-Module Microsoft.Graph
    

    然后:

    Connect-Graph <follow instructions to authenticate your commands>
    Update-MgRoleManagementDirectoyRoleAssignment -PrincipalID <your service princiapl> -RoleDefinitionID <Role Definition>
    

    Microsoft.Graph.Identity 模块的文档似乎很少/不存在,但您可以在此处查看 Microsoft Graph API 描述以获取指导:https://docs.microsoft.com/en-us/azure/active-directory/users-groups-roles/roles-assign-graph

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-28
      • 1970-01-01
      • 2018-01-21
      • 2013-02-18
      • 1970-01-01
      • 2020-05-21
      • 2013-12-06
      相关资源
      最近更新 更多