使用 PowerShell 导出所有 Microsoft 365 用户 MFA 状态
在这篇文章中,我将向您展示如何使用 PowerShell 导出有关 Microsoft 365 中所有用户的 MFA 状态的报告。这将为您提供清晰的概述用户 MFA 设置的当前状态。
如果你计划在环境中分配条件访问以强制执行 MFA,但没有 Azure 订阅来启用日志分析,那么以清晰简洁的方式查看用户 MFA 状态也特别有帮助。
查看用户 MFA 状态的唯一其他选项是使用 Microsoft 365 管理中心中的多重身份验证用户网页,但这确实缺乏任何有用的功能,例如导出、过滤或识别正在使用的身份验证方法。
此外,如果您想了解如何强制使用更安全的身份验证方法,例如阻止使用短信身份验证,请查看我的关于如何启用 require 的教程条件访问中的身份验证强度设置。
先决条件
要运行此脚本,您必须拥有 Microsoft 365 租户的全局管理员访问权限。这是为了确保您可以阅读将导出的所有相关信息。 如果您不确定自己是否具有此级别的访问权限,请与您的 IT 团队联系或要求他们代表您运行此脚本。
您还应该安装 MSOnline PowerShell 模块。如果您不确定如何执行此操作,请查看我的帖子:如何修复术语“Connect-MsolService”无法识别
使用 PowerShell 导出用户 MFA 状态
导出每个用户的 MFA 信息的脚本相当简单。它将首先连接到 MSOnline 服务,您将立即看到一个交互式提示,要求您登录。登录后,它将收集租户中所有用户帐户的列表,然后循环访问每个用户以存储 MFA 信息。完成后,信息将导出到系统上的 csv 文件,路径为 C:\temp
您可以将以下脚本复制并粘贴到 PowerShell 中,也可以将其另存为 .ps1 文件并以这种方式运行。
#Connect to Microsoft Online
Connect-MsolService
#Get all user accounts
Write-Host "Finding accounts..." -ForegroundColor white -BackgroundColor Black
$Users = Get-MsolUser -All | Where-Object { $_.UserType -ne "Guest" }
#create a new object
$Report = [System.Collections.Generic.List[Object]]::new()
#pause and display information
Start-Sleep 1.5
Write-Host $users.count "Accounts found" -ForegroundColor white -BackgroundColor Black
#Loop through each user, gather information on mfa status and methods and add to report
ForEach ($user in $users) {
$mfadefaultmethod = ($User.StrongAuthenticationMethods | Where-Object { $_.IsDefault -eq "True"}).methodtype
$userupn = $user.UserPrincipalName
$displayname = $user.displayname
If (($user.StrongAuthenticationRequirements.state) -eq "Enforced") {
$state = $User.StrongAuthenticationRequirements.State
}
Else {
$state = 'Disabled'
}
If ($mfadefaultmethod) {
Switch ($mfadefaultmethod) {
"OneWaySMS" { $Method = "SMS" }
"TwoWayVoiceMobile" { $Method = "Phone call" }
"PhoneAppOTP" { $Method = "Hardware token or authenticator app" }
"PhoneAppNotification" { $Method = "Authenticator app" }
}
}
Else {
$mfadefaultmethod = "Not set"
}
$ReportLine = [PSCustomObject] @{
UserPrincipalName = $User.UserPrincipalName
DisplayName = $User.DisplayName
MFAState = $State
MFADefaultMethod = $MFADefaultMethod
}
$Report.Add($ReportLine)
}
#Export report to CSV file
$Report | Export-CSV -Encoding UTF8 -NoTypeInformation "c:\temp\m365mfareport.csv"
Write-Host "Data exported to C:\temp folder" -ForegroundColor white -BackgroundColor Black