使用 Microsoft Graph PowerShell 更新默认 MFA 方法
默认 MFA 方法定义了当您对 Microsoft 365 帐户执行交互式登录并且强制执行 MFA(多重身份验证)时发送给您的初始质询。
虽然在整个组织中部署和实施 MFA 是迈向更好安全性的重要一步,但一旦部署,维护往往会被忽视。例如,您是否知道您的用户在注册更安全的方法(例如 Microsoft 身份验证应用程序)之前部署了哪些其他身份验证方法?如果不这样做,您可能不知道他们在交互式登录挑战期间首选的方法是什么。
转向一致的 MFA 状态的第一步是确保注册了更强方法的用户在登录期间将其设置为主要身份验证方法。其他选项,例如系统首选的 MFA 方法,但是,本指南是针对可能尚未准备好采取该举措或希望分阶段推出的组织量身定制的。
在本教程中,我将向您展示如何使用 Microsoft Graph PowerShell 更新使用 Microsoft Graph PowerShell 的用户的默认身份验证方法。
先决条件
要运行本教程中的命令,您必须确保可以在脚本中使用以下权限:
- UserAuthenticationMethod.ReadWrite.All
- 用户.阅读.全部
您可以在首次运行脚本期间使用全局管理员帐户同意这些权限,也可以先将这些权限分配给您的 Microsoft Graph 命令行工具应用程序,然后与在 中拥有身份验证策略管理员角色的帐户连接。蔚蓝广告。
您还必须确保安装了 Microsoft Graph PowerShell SDK,如果安装了,请确保它是最新的可用版本。请按照我的指南进行操作:如何安装 Microsoft Graph PowerShell 模块
所有 MFA 方法
对于本教程,在下面的脚本中,我决定将 push 方法部署为每个用户的默认 MFA 方法。然而,还有其他选项可用,尽管有些选项是强烈不推荐的,但您可以在下面看到它们:
- 推送 - Microsoft Authenticator 推送通知并进行号码匹配。
- 誓言 - 带有身份验证应用程序的 6 位 (OTP) 密码。
- voiceMobile - 使用 6 位数代码应答语音呼叫。
- voiceAlternateMobile - 在备用移动设备上使用 6 位数代码接听语音呼叫。
- voiceOffice - 使用 6 位数代码在办公室电话上接听语音呼叫。
- sms - 带有 6 位数代码的短信。
- unknownFutureValue - 不支持的值。
更新单个用户的默认 MFA 方法
要更新组织中单个用户的默认 MFA 方法,请首先使用 UserAuthenticationMethod.ReadWrite.All 权限范围连接到 Microsoft Graph。
Connect-MgGraph -scopes UserAuthenticationMethod.ReadWrite.All然后定义请求正文,这将确定您将为用户设置的首选 MFA 方法。
$body = @'
{
"userPreferredMethodForSecondaryAuthentication": "push"
}
'@我们还需要定义请求的 URI,该 URI 指定了我们要更新的资源。首先,我建议您使用 Get-MgUser 命令来存储有关用户的信息,然后将此信息传递到 URI 中。
$user = Get-MgUser -filter "UserPrincipalName eq '[email '"
$uri = "https://graph.microsoft.com/beta/users/$($user.id)/authentication/signInPreferences"要进行更改,您需要将 Invoke-MgGraphRequest cmdlet 与 PATCH 方法结合使用,因为目前没有支持此更改的 cmdlet。
Invoke-MgGraphRequest -uri $uri -Body $body -Method PATCH更新所有用户的默认 MFA 方法
您可能想要更新组织中多个用户的默认 MFA 方法。当然,如果您想应用于所有用户,您最好应用系统首选的 MFA 上下文设置。但是,此方法仍然很有用,特别是如果您想确保特定部门中的多个用户具有正确的默认方法集。
有关如何自定义 Get-MgUser 命令以获取一组特定用户的更多详细信息,请参阅我的指南:如何将 Get-MgUser 与 Microsoft Graph PowerShell 结合使用。
Connect-MgGraph -scopes UserAuthenticationMethod.ReadWrite.All, User.Read.All
$allusers = Get-MgUser -all
$body = @'
{
"userPreferredMethodForSecondaryAuthentication": "placeholder"
}
'@
$RegisteredMethod = "microsoftAuthenticator" # Check method type
$PreferredMethod = "push" # Define the preferred MFA method here
$body = $body -replace 'placeholder', $preferredmethod
Foreach ($user in $allusers) {
$uri = "https://graph.microsoft.com/beta/users/$($user.id)/authentication/signInPreferences"
$Check = Invoke-MgGraphRequest -uri $uri -Method GET -OutputType PSObject
If ($Check.userPreferredMethodForSecondaryAuthentication -eq $PreferredMethod){
Write-host "`n $($user.DisplayName) already has preferred method set to $PreferredMethod, Skipping..." -ForegroundColor Cyan
Continue
}
If ((Get-MgUserAuthenticationMethod -UserId $user.id).AdditionalProperties.values -like $RegisteredMethod){
try {
Invoke-MgGraphRequest -uri $uri -Body $body -Method PATCH -ErrorAction Stop
}
Catch {
Write-Host "`n Unable to update user authentication method for $($user.DisplayName)"
Continue
}
Write-host "Default MFA method has been successfully updated for $($user.DisplayName)" -ForegroundColor Green
} Else {
Write-Host "`n $RegisteredMethod has not been registered by $($user.UserPrincipalName)"-ForegroundColor Yellow
}
}修复错误:UserPreferredMethodForSecondaryAuthentication 无法 更新
如果您尝试更新用户并收到以下错误:
UserPreferredMethodForSecondaryAuthentication 无法更新
您尝试更新的用户可能尚未注册必要的 MFA 方法。您可以通过多种方法来验证他们设置了哪些方法,第一种是通过 PowerShell,运行以下代码以查看已为用户配置了哪些方法
(Get-MgUserAuthenticationMethod -UserId %upn%).AdditionalProperties.values或者,可以通过 Azure Active Directory 门户找到相同的信息。 首先,转到Azure Active Directory > 保护和安全 > 认证方式 > 用户注册详情。
然后在列表中找到您的目标用户并查看已注册的方法列。