编程开源技术交流,分享技术与知识

网站首页 > 开源技术 正文

使用 POWERSHELL 为 MICROSOFT 365 用户获取 MFA 状态

wxchong 2024-08-01 02:55:31 开源技术 18 ℃ 0 评论

没有便捷的方法可以查明是否为特定用户启用了多重身份验证以及在 Microsoft 365 管理中心 Web 界面中配置了哪些 MFA 方法。在本文中,我们将向您展示如何使用 PowerShell 获取 Microsoft 365 用户的 MFA 状态。

以前,您可以使用 MsOnline 模块中的 Get-MsolUser cmdlet 或 AzureAD 模块中的 Get-AzureADUser cmdlet 来获取 Microsoft 365 用户的 MFA 状态。但是,2023 年 6 月 30 日之后,旧版 Azure AD 和 MSOL 模块(使用 Azure AD API)已被弃用。因此,现在必须使用 Microsoft Graph PowerShell 模块来获取 Azure 租户中的用户 MFA 状态。

为计算机上的所有用户安装 PowerShell 模块 Microsoft Graph:

Install-Module Microsoft.Graph -Scope AllUsers –force

您可以检查您的计算机是否安装了 Microsoft Graph:

Find-Module Microsoft.Graph

然后使用 Microsoft Graph 模块连接到您的租户:

Connect-MgGraph -Scopes "User.Read.All,UserAuthenticationMethod.Read.All"

进行身份验证并授予 Microsoft Graph 命令行工具读取所有 Azure 用户属性和身份验证方法的权限。

如果您已通过 PowerShell 成功通过 Azure 身份验证,您应该会看到消息“欢迎使用 Microsoft Graph!

运行以下命令获取单个用户的 MFA 状态:

Get-MGUserAuthenticationMethod -userid kirill@theitbros.onmicrosoft.com | fl

如果您未授予对 UserAuthenticationMethod.Read 范围的访问权限,则会发生错误:

Get-MgUserAuthenticationMethod_List1: Request Authorization failed

在此示例中,仅为用户启用了 microsoft.graph.passwordAuthenticationMethod。这意味着未配置 MFA。

如果用户配置了任何其他 MFA 方法,Get-MGUserAuthenticationMethod cmdlet 会列出它们。

例如,我的帐户配置为使用移动设备上的 Microsoft Authenticator 应用进行身份验证 (#microsoft.graph.microsoftAuthenticatorAuthenticationMethod)。

可以为用户配置其他强大的 MFA 身份验证方法,包括生物识别、身份验证器应用程序、硬件安全密钥等。

如果为用户配置了多个 MFA 方法,则命令输出可能很难理解启用了哪些方法。

因此,您可以使用这个简单的 PowerShell 脚本来检查单个用户的 MFA 状态。该脚本检查是否为用户配置了除“密码身份验证”之外的任何强身份验证方法。

$AzureUserUPN='enterprise@qoot.cool'

$MFAUserData=Get-MgUserAuthenticationMethod -UserId $AzureUserUPN

$AzureUserObject = [PSCustomObject]@{

    "User"                        = 'n/a'

    "MFA status"                  = "Disabled"

    "Email authentication"        = "False"

    "FIDO2 authentication"        = "False"

    "Microsoft Authenticator"     = "False"

    "Password authentication"     = "False"

    "Phone authentication"        = "False"

    "Software Oath"               = "False"

    "Temporary Access Pass"       = "False"

    "Windows Hello for Business"  = "False"

    "Passwordless Authenticator"  = "False"

}

$AzureUserObject.user = $AzureUserUPN

foreach ($method in $MFAUserData) {

    Switch ($method.AdditionalProperties["@odata.type"]) {

        "#microsoft.graph.microsoftAuthenticatorAuthenticationMethod" {

            $AzureUserObject."Microsoft Authenticator" = $true

            $AzureUserObject."MFA status" = "Enabled"

        }

        "#microsoft.graph.emailAuthenticationMethod" {

            $AzureUserObject."Email authentication" = $true

            $AzureUserObject."MFA status" = "Enabled"

        }

        "#microsoft.graph.passwordAuthenticationMethod" {

            $AzureUserObject."Password authentication" = $true

            if ($AzureUserObject."MFA status" -ne "Enabled") {

                $AzureUserObject."MFA status" = "Disabled"

            }

        }

        "#microsoft.graph.fido2AuthenticationMethod" {

            $AzureUserObject."FIDO2 authentication" = $true

            $AzureUserObject."MFA status" = "Enabled"

        }

        "#microsoft.graph.phoneAuthenticationMethod" {

            $AzureUserObject."Phone authentication" = $true

            $AzureUserObject."MFA status" = "Enabled"

        }

        "#microsoft.graph.softwareOathAuthenticationMethod" {

            $AzureUserObject."Software Oath" = $true

            $AzureUserObject."MFA status" = "Enabled"

        }

        "#microsoft.graph.temporaryAccessPassAuthenticationMethod" {

            $AzureUserObject."Temporary Access Pass" = $true

            $AzureUserObject."MFA status" = "Enabled"

        }

        "#microsoft.graph.windowsHelloForBusinessAuthenticationMethod" {

            $AzureUserObject."Windows Hello for Business" = $true

            $AzureUserObject."MFA status" = "Enabled"

        }

    }

}    

$AzureUserObject

该脚本将返回 MFA 状态:如果用户配置为仅使用密码身份验证,则已禁用。

如果除了密码身份验证之外还配置了一种或多种强身份验证方法,则意味着为用户启用了 MFA。

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表