当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 使用 Powershell 导出 Azure AD 中的所有管理员角色成员资格

作者:精品下载站 日期:2024-12-14 06:49:40 浏览:15 分类:玩电脑

使用 Powershell 导出 Azure AD 中的所有管理员角色成员资格


导出 Azure AD 中所有分配的管理员角色的列表的功能将为您提供租户中特权用户的顶层概述。这将使您能够就是否向相关用户分配适当的权限,或者是否需要进行一些更改做出明智的决定。

在之前的教程中,我演示了如何使用 PowerShell 从 PIM(特权身份管理)导出管理员角色分配的完整列表,您可以在此处阅读本教程:如何使用 Microsoft Graph PowerShell 导出所有 AzureAD PIM 角色。

在本教程中,我将向您展示如何使用 Microsoft Graph PowerShell 运行 Azure AD 中所有管理员角色成员身份的报告并将其导出为 CSV。如果您不使用 PIM 进行角色管理,但仍需要清楚地识别哪些用户在租户中拥有管理员角色成员身份,那么这会非常有用。

先决条件

电源外壳

若要运行此脚本中的命令,您必须从模块的版本 1 安装最新版本的 Microsoft Graph PowerShell。版本 2 不支持此脚本中使用的 Get-MgUser cmdlet 的某些信息。

有关如何安装 Microsoft Graph PowerShell 的指南,请按照我的教程进行操作:如何安装 Microsoft Graph PowerShell 模块

权限

要同意 Microsoft Graph PowerShell 的必要权限(RoleManagement.Read.Directory、User.Read.All、AuditLog.Read.All),您需要在租户中分配全局管理员角色。但是,一旦同意了权限,就可以使用较低层的用户上下文,例如全局读取器。

导出所有管理员角色成员资格脚本

该脚本将首先连接到 Microsoft Graph 并存储所有特权目录角色,将它们保存到 $allroles 变量中。然后,它将循环遍历每个角色并检查是否有成员分配,如果没有分配成员,则将跳过角色。然后将配置一个数组对象,其中包含当前角色的每个分配的信息,然后将其添加到报告中。

您还可以在此处从我的 Github 个人资料访问此脚本的相同副本。

此脚本中使用了 3 个核心 cmdlet:

  • Get-MgDirectoryRole > 此 cmdlet 将检索租户中的所有管理员角色。
  • Get-MgDirectoryRoleMember > 此 cmdlet 将检索特定角色的成员。
  • Get-MgUser > 此 cmdlet 将检索租户中的用户。我在这里编写了有关使用此 cmdlet 的综合指南:How To Use Get-MgUser with Microsoft Graph PowerShell
使用这个脚本要使用该脚本,我建议将光标悬停在下面的脚本上,然后使用右上角的复制功能。然后将脚本粘贴到 IDE、PowerShell ISE 或记事本中,并确保您了解它在做什么。如果您满意,可以从 PowerShell ISE 运行它或将其粘贴到您的 PowerShell 会话中。
<#
AUTHOR: Daniel Bradley
LINKEDIN: https://www.linkedin.com/in/danielbradley2/
TWITTER: https://twitter.com/DanielatOCN
WEBSITE: https://ourcloudnetwork.com/
Info: This script was written by Daniel Bradley for the ourcloudnetwork.com blog
#>

#Connect to Microsoft Graph
Connect-MgGraph -Scopes RoleManagement.Read.Directory, User.Read.All, AuditLog.Read.All
Select-MgProfile -Name Beta

#Get all directory roles
$allroles = Get-MgDirectoryRole

#Provision in new array object
$Report = [System.Collections.Generic.List[Object]]::new()

#Start a loop to build the report
Foreach ($role in $allroles){
    $rolemembers = $null
    #Get members of each role
    $Rolemembers = Get-MgDirectoryRoleMember -DirectoryRoleId $Role.id
    #Skip role if role assignments are empty
    If ($Rolemembers -eq $null) {Write-host "No users assigned to $($Role.DisplayName)"} Else {
        Foreach ($Member in $rolemembers){
        #Filter out non-user assignments
            If ($member.AdditionalProperties.'@odata.type' -notmatch "servicePrincipal") {
                $SignInActivity = $null
                #Get signin logs for user
                $SignInActivity = Get-MgUser -UserId $member.id -Property signinactivity | Select-Object -ExpandProperty signinactivity
                #Build current array object
                $obj = [pscustomobject][ordered]@{
                    Role                     = $Role.DisplayName
                    User                     = $member.AdditionalProperties.displayName
                    Username                 = $member.AdditionalProperties.userPrincipalName
                    LastInteractiveSignIn    = $SignInActivity.LastSignInDateTime
                }
                #Add current array object to the report
                $report.Add($obj)
            }
        }
    }
}

#Export report to csv
$report | Export-CSV -path C:\temp\AdminRoleReport.csv -NoTypeInformation

概括

此脚本是使用 Microsoft Graph PowerShell 获取可操作的管理员角色成员资格报告的简单准系统方法。它绝不是针对您的角色成员身份的完整审核解决方案,但是,它将帮助您采取第一步并通过取消用户不再需要的访问权限来减少管理足迹,从而开始更好的角色管理之旅。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯