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

在许多组织中,管理员被添加到不同的管理角色组中。在大多数情况下,这些都是高私有的角色。但是,他们不需要那么多访问权。最好创建一个管理员角色报告,并查看添加了哪个成员的管理员角色。在本文中,您将学习如何用Powershell和Microsoft 365 Admin Center导出Microsoft 365 Admin角色成员报告。

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

  1. 登录到Microsoft 365管理中心
  2. 点击角色>角色分配
  3. 选择Microsoft 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. DisplayName
  3. 用户principalname
  4. 会员类型
  5. LastSuccessfulSignIndateTime(需要Microsoft Entra ID P1/P2许可证)
  6. AccountStatus
  7. 许可

要将所有管理角色成员带有PowerShell,请按照以下步骤:

步骤1。安装Microsoft Graph PowerShell

运行Windows PowerShell作为管理员,并安装Microsoft Graph PowerShell模块。

Install-Module Microsoft.Graph -Force

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

步骤2。下载导出-M365Adminroles PowerShell脚本

(c :)驾驶:

  • 脚本
  • 温度

下载并将Export-M365Adminroles.ps1 PowerShell脚本放在C:脚本文件夹。该脚本将向所有用户指出所有用户角色的用户C:温度文件夹。

确保文件未阻止以防止运行脚本时的错误。在运行PowerShell脚本时,请在文章中阅读更多信息。

有关的:Microsoft 365中的Export OnEdrive用法报告

另一个选择是将下面的代码复制并粘贴到记事本中。将其称为“导出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。运行导出M365Adminroles PowerShell脚本

在下面运行命令以运行export-m365adminroles.ps1PowerShell脚本并创建报告。

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

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

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

export-m365adminroles.ps1 powershell脚本将所有用户带有管理角色到CSV文件。

查找文件AdminRolesReport.csv在路径中C:温度

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

就是这样!

结论

您学会了如何导出Microsoft 365 Admin角色成员报告。虽然可以从Microsoft 365 Admin Center中导出所有管理角色成员,但最好将PowerShell用于报告。那是因为您将看到所有用户,而不是任何群体作为成员。另外,您可以将您喜欢的各种属性添加到导出CSV文件中,这非常适合报告。

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