导出 Microsoft 365 管理员角色成员报告

Jacki

在许多组织中,管理员被添加到不同的管理角色组中。大多数时候,这些都是高特权角色。但是,他们不需要那么多访问权限。最好创建管理员角色报告并查看哪个成员添加到哪个管理角色。在本文中,您将了解如何使用 PowerShell 和 Microsoft 365 管理中心导出 Microsoft 365 管理角色成员报告。

要导出 Microsoft 365 中的管理员角色成员,请按照以下步骤操作:

  1. 登录到Microsoft 365 管理中心
  2. 点击角色 > 角色分配
  3. 选择微软Entra ID
  4. 点击导出管理员列表
  1. 打开 Microsoft Entra ID/Microsoft 365 管理员角色报告 CSV 文件

此报告的缺点是它不显示组中的用户。这时候 PowerShell 就可以派上用场了。我们来看看。

使用 PowerShell 脚本导出管理员角色成员

Export-M365AdminRoles.ps1 PowerShell 脚本将获取 Microsoft 365/Entra 租户中具有管理员角色的所有用户。之后,它将报告导出为 CSV 文件。您可以使用 Microsoft Excel 或支持 CSV 文件扩展名的任何其他应用程序打开 CSV 文件。

该脚本将收集以下信息:

  1. 角色
  2. 显示名称
  3. 用户主体名称
  4. 会员类型
  5. LastSuccessfulSignInDateTime(需要 Microsoft Entra ID P1/P2 许可证)
  6. 账户状态
  7. 许可状态

要使用 PowerShell 导出所有管理员角色成员,请按照以下步骤操作:

步骤 1. 安装 Microsoft Graph PowerShell

以管理员身份运行 Windows PowerShell 并安装 Microsoft Graph PowerShell 模块。

Install-Module Microsoft.Graph -Force

重要的:在运行 cmdlet 或脚本之前,请务必更新到最新的 Microsoft Graph PowerShell 模块版本,以防止出现错误和不正确的结果。

步骤 2. 下载 Export-M365AdminRoles PowerShell 脚本

上创建两个文件夹(中:)驾驶:

  • 脚本
  • 温度

下载 Export-M365AdminRoles.ps1 PowerShell 脚本并将其放置在C:脚本文件夹。该脚本会将所有具有管理员角色的用户导出到C:温度文件夹。

确保文件未被阻止,以防止运行脚本时出现错误。请阅读文章运行 PowerShell 脚本时出现未数字签名错误来了解更多信息。

另一种选择是将以下代码复制并粘贴到记事本中。将其命名为 Export-M365AdminRoles.ps1 并将其放置在C:脚本文件夹。

<#
    .SYNOPSIS
    Export-M365AdminRoles.ps1

    .DESCRIPTION
    Export all Microsoft 365/Microsoft Entra ID admin roles members to CSV file.

    .LINK
    www.alitajran.com/export-microsoft-365-admin-roles-members/

    .NOTES
    Written by: ALI TAJRAN
    Website:    www.alitajran.com
    LinkedIn:   linkedin.com/in/alitajran
    X:          x.com/alitajran

    .CHANGELOG
    V2.00, 02/10/2025 - Refactored the script for faster performance.
#>

param (
    [Parameter(Mandatory = $true)]
    [string]$ExportPath
)

# Connect to Microsoft Graph with necessary scopes
Connect-MgGraph -Scopes "RoleManagement.Read.Directory", "User.Read.All", "Group.Read.All", "AuditLog.Read.All", "Organization.Read.All" -NoWelcome

# Define CSV file export location variable using the parameter provided
$Csvfile = "$ExportPath"

# Retrieve all subscribed SKUs and check for Entra ID Premium
$hasPremium = $false
$hasPremium = (Get-MgSubscribedSku).ServicePlans.ServicePlanName -contains "AAD_PREMIUM"

# Display status of Premium subscription
if ($hasPremium) {
    Write-Host "Microsoft Entra ID Premium subscription available." -ForegroundColor Cyan
}
else {
    Write-Host "Microsoft Entra ID Premium subscription unavailable." -ForegroundColor Cyan
}

# If Premium is available, include the sign-in activity for the user
$propertyParams = if ($hasPremium) {
    @('AccountEnabled', 'AssignedLicenses', 'SignInActivity')
}
else {
    @('AccountEnabled', 'AssignedLicenses')
}

# Initialize a List to store the data
$Report = [System.Collections.Generic.List[Object]]::new()

# Get all directory roles
$allroles = Get-MgDirectoryRole | Select-Object Id, DisplayName

# Initialize a counter for progress bar
$totalRoles = $allroles.Count
$counter = 0

# Loop through each user to check for role memberships with progress bar
foreach ($role in $allroles) {
    $counter++
    $percentComplete = [math]::Round(($counter / $totalRoles) * 100, 2)
    Write-Progress -Activity "Processing Roles - $percentComplete%" -Status "Role: $($role.DisplayName)" -PercentComplete $percentComplete

    # Get members of each role
    $Rolemembers = Get-MgDirectoryRoleMember -DirectoryRoleId $role.id
    if ($null -ne $Rolemembers) {
        foreach ($Member in $rolemembers) {
            $odataType = $member.AdditionalProperties.'@odata.type'
            switch -regex ($odataType) {
                'user$' {
                    # Get user information including potential sign-in activity
                    $userInfo = Get-MgUser -UserId $member.id -Property $propertyParams | Select-Object $propertyParams
                    $lastSignIn = if ($hasPremium) {
                        if ($userInfo.SignInActivity -and $userInfo.SignInActivity.LastSuccessfulSignInDateTime) {
                            $userInfo.SignInActivity.LastSuccessfulSignInDateTime
                        }
                        else {
                            "Never Signed In"
                        }
                    }
                    else {
                        "Microsoft Entra ID Premium license unavailable"
                    }
                    $memberType = "User"
                    $username = $member.AdditionalProperties.userPrincipalName
                    $accountStatus = if ($userInfo.AccountEnabled) { "Enabled" } else { "Disabled" }
                    $licenseStatus = if ($userInfo.AssignedLicenses) { "Licensed" } else { "Unlicensed" }
                }
                'group$' {
                    $lastSignIn = "N/A"
                    $memberType = "Group"
                    $username = "N/A"
                    $accountStatus = "N/A"
                    $licenseStatus = "N/A"
                }
                default {
                    $lastSignIn = "N/A"
                    $memberType = $odataType.Split('#')[1]
                    $username = "N/A"
                    $accountStatus = "N/A"
                    $licenseStatus = "N/A"
                }
            }

            # Create custom object for report
            $ReportLine = [PSCustomObject][Ordered]@{
                Role                         = $role.DisplayName
                DisplayName                  = $member.AdditionalProperties.displayName
                UserPrincipalName            = $username
                MemberType                   = $memberType
                LastSuccessfulSignInDateTime = $lastSignIn
                AccountStatus                = $accountStatus
                LicenseStatus                = $licenseStatus
            }
            $Report.Add($ReportLine)
        }
    }
}

# Finish the progress bar
Write-Progress -Activity "Processing Roles" -Status "Completed" -PercentComplete 100 -Completed

# Show report in Out-GridView and export report to CSV file
if ($Report.Count -gt 0) {
    $Report | Out-GridView -Title "Microsoft 365 Role Membership Report"
    $Report | Export-Csv -Path $Csvfile -NoTypeInformation -Encoding Utf8
    Write-Host "Report saved at $Csvfile" -ForegroundColor Cyan
}
else {
    Write-Host "No roles were found for any users." -ForegroundColor Yellow
}

步骤 3. 运行 Export-M365AdminRoles PowerShell 脚本

运行以下命令来运行导出-M365AdminRoles.ps1PowerShell 脚本并创建报告。

了解更多:如何在 Microsoft Entra ID 中导出 PIM 角色

C:scripts.Export-M365AdminRoles.ps1 -ExportPath "C:tempAdminRolesReport.csv"

报告输出将发送到单独窗口 (Out-GridView) 中的交互式表格。

步骤 4. 检查管理员角色报告 CSV 文件

Export-M365AdminRoles.ps1 PowerShell 脚本将所有具有管理员角色的用户导出到 CSV 文件。

找到文件管理员角色报告.csv在路径中C:温度

使用您喜欢的应用程序打开 CSV 文件。在我们的示例中,它是 Microsoft Excel。

就是这样!

结论

您了解了如何导出 Microsoft 365 管理员角色成员报告。虽然可以从 Microsoft 365 管理中心导出所有管理角色成员,但最好使用 PowerShell 来生成报告。这是因为您将看到所有用户而不是任何组作为成员。此外,您还可以将您喜欢的各种属性添加到导出的 CSV 文件中,这对于报告来说是完美的。

您喜欢这篇文章吗?您可能还喜欢如何配置 Microsoft Entra 特权身份管理 (PIM)。不要忘记关注我们并分享这篇文章。