導出 Microsoft 365 管理員角色成員報告
在許多組織中,管理員被添加到不同的管理角色組中。大多數時候,這些都是高特權角色。但是,他們不需要那麼多訪問權限。最好創建管理員角色報告並查看哪個成員添加到哪個管理角色。在本文中,您將了解如何使用 PowerShell 和 Microsoft 365 管理中心導出 Microsoft 365 管理角色成員報告。
要導出 Microsoft 365 中的管理員角色成員,請按照以下步驟操作:
- 登錄到Microsoft 365 管理中心
- 點擊角色 > 角色分配
- 選擇微軟Entra ID
- 點擊導出管理員列表
- 打開 Microsoft Entra ID/Microsoft 365 管理員角色報告 CSV 文件
此報告的缺點是它不顯示組中的用戶。這時候 PowerShell 就可以派上用場了。我們來看看。
使用 PowerShell 腳本導出管理員角色成員
Export-M365AdminRoles.ps1 PowerShell 腳本將獲取 Microsoft 365/Entra 租戶中具有管理員角色的所有用戶。之後,它將報告導出為 CSV 文件。您可以使用 Microsoft Excel 或支持 CSV 文件擴展名的任何其他應用程序打開 CSV 文件。
該腳本將收集以下信息:
- 角色
- 顯示名稱
- 用戶主體名稱
- 會員類型
- LastSuccessfulSignInDateTime(需要 Microsoft Entra ID P1/P2 許可證)
- 賬戶狀態
- 許可狀態
要使用 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)。不要忘記關注我們並分享這篇文章。
