当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 使用 PowerShell 获取 MFA 状态(包含脚本)

作者:精品下载站 日期:2024-12-14 22:27:12 浏览:15 分类:玩电脑

使用 PowerShell 获取 MFA 状态(包含脚本)


在本指南中,您将了解如何使用 PowerShell 获取 Office 365 用户的 MFA 状态。

我将向您展示如何获取单个用户和一组用户的 MFA 状态。

此外,我将向您展示如何将 Office 365 MFA 状态报告导出为 CSV。

让我们开始吧。

通过 PowerShell 要求获取 MFA 状态

本指南中的示例使用 Microsoft Graph 模块检查 Office 365 用户的 MFA 状态。您需要安装图形模块。

Get-MGUser cmdlet 用于从 Office 365 租户获取单个和所有用户。

Get-MGUserAuthenticationMethod cmdlet 用于获取每个用户的 MFA 身份验证方法。

重要的

PowerShell 命令报告为每个用户注册的身份验证方法,这就是确定 MFA 状态的方式。不幸的是,微软没有提供一个命令来简单说明帐户是否启用了 MFA,必须进行计算。

当passwordAuthenticationMethod 是列出的唯一身份验证方法时,这意味着用户未启用MFA。我在下面提供的脚本将检查身份验证方法并创建 MFA 状态字段(启用或禁用)。

示例 1. 获取单个用户的 Office 365 MFA 状态

检查单个用户的 MFA 状态非常简单,您不需要为此使用臃肿的脚本。

步骤 1. 连接到 Microsoft Graph

在获取 Office 365 用户并检查 MFA 状态之前,您首先需要连接到 Microsoft Graph。

以下命令将允许您读取完整的 Azure 用户配置文件属性集。

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

注意:一些用户提到脚本失败,请求授权失败。在这种情况下,请尝试使用 Connect-MgGraph -Scopes “UserAuthenticationMethod.Read.All”

系统将提示您使用您的帐户登录。

[玩转系统] 使用 PowerShell 获取 MFA 状态(包含脚本)

经过身份验证后,PowerShell 应显示“欢迎使用 Microsoft Graph!”

[玩转系统] 使用 PowerShell 获取 MFA 状态(包含脚本)

步骤 2. 运行 Get-MGUserAuthenticationMethod cmdlet

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

Get-MGUserAuthenticationMethod -userid [email protected] | fl

在此示例中,我正在检查用户 [email protected] 的 MFA 状态。

[玩转系统] 使用 PowerShell 获取 MFA 状态(包含脚本)

microsoft.graph.passwordAuthenticationMethod 的身份验证方法是列出的唯一方法,这意味着未为此用户启用 MFA。

现在我将检查我帐户的身份验证方法。

[玩转系统] 使用 PowerShell 获取 MFA 状态(包含脚本)

在上面的屏幕截图中,您可以看到我的帐户返回多种身份验证方法,这意味着我的帐户启用了 MFA。

检查所有用户时,它会变得更加复杂,好消息是我已经创建了一个您可以使用的脚本。

示例 2. 所有用户 PowerShell 的 MFA 状态 Office 365 报告

您可以复制下面的脚本或下载 MFAStatusReport.ps1 PowerShell 脚本。

默认情况下,该脚本将获取所有用户的 MFA 状态。我将向您展示如何更改它以检查用户列表的状态。

注意:根据租户中的用户数量,脚本可能需要几分钟才能完成。

<#
=============================================================================================
Name:           Get MFA Status Report
Description:    Gets MFA status for all users and authentication methods
Version:        1.0
Website:        activedirectorypro.com
Script by:      activedirectorypro.com
Instructions:   https://activedirectorypro.com/mfa-status-powershell
============================================================================================
#>

#Get all Azure users
$users = get-mguser -All

$results=@();
Write-Host  "`nRetreived $($users.Count) users";
#loop through each user account
foreach ($user in $users) {

Write-Host  "`n$($user.UserPrincipalName)";
$myObject = [PSCustomObject]@{
    user               = "-"
    MFAstatus          = "_"
    email              = "-"
    fido2              = "-"
    app                = "-"
    password           = "-"
    phone              = "-"
    softwareoath       = "-"
    tempaccess         = "-"
    hellobusiness      = "-"
}

$MFAData=Get-MgUserAuthenticationMethod -UserId $user.UserPrincipalName #-ErrorAction SilentlyContinue

$myobject.user = $user.UserPrincipalName;
    #check authentication methods for each user
    ForEach ($method in $MFAData) {
	
        Switch ($method.AdditionalProperties["@odata.type"]) {
          "#microsoft.graph.emailAuthenticationMethod"  { 
             $myObject.email = $true 
             $myObject.MFAstatus = "Enabled"
          } 
          "#microsoft.graph.fido2AuthenticationMethod"                   { 
            $myObject.fido2 = $true 
            $myObject.MFAstatus = "Enabled"
          }    
          "#microsoft.graph.microsoftAuthenticatorAuthenticationMethod"  { 
            $myObject.app = $true 
            $myObject.MFAstatus = "Enabled"
          }    
          "#microsoft.graph.passwordAuthenticationMethod"                {              
                $myObject.password = $true 
                # When only the password is set, then MFA is disabled.
                if($myObject.MFAstatus -ne "Enabled")
                {
                    $myObject.MFAstatus = "Disabled"
                }                
           }     
           "#microsoft.graph.phoneAuthenticationMethod"  { 
            $myObject.phone = $true 
            $myObject.MFAstatus = "Enabled"
          }   
            "#microsoft.graph.softwareOathAuthenticationMethod"  { 
            $myObject.softwareoath = $true 
            $myObject.MFAstatus = "Enabled"
          }           
            "#microsoft.graph.temporaryAccessPassAuthenticationMethod"  { 
            $myObject.tempaccess = $true 
            $myObject.MFAstatus = "Enabled"
          }           
            "#microsoft.graph.windowsHelloForBusinessAuthenticationMethod"  { 
            $myObject.hellobusiness = $true 
            $myObject.MFAstatus = "Enabled"
          }                   
        }
    }

##Collecting objects
$results+= $myObject;

}
# Display the custom objects
$results

要运行该脚本,请打开 PowerShell 并首先连接到 MS Graph。

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

然后输入脚本的路径和名称来执行它。

该脚本将显示它找到的帐户数量并输出它正在处理的帐户。

[玩转系统] 使用 PowerShell 获取 MFA 状态(包含脚本)

脚本完成后,它将显示每个用户的 MFA 状态和身份验证方法。

[玩转系统] 使用 PowerShell 获取 MFA 状态(包含脚本)

要将 MFA 状态报告导出为 CSV,请使用导出 CSV 参数。

.\MFAStatusReport.ps1 | export-csv -path c:\it\mfastatus-csv

[玩转系统] 使用 PowerShell 获取 MFA 状态(包含脚本)

检查用户列表的 MFA 状态

如果您想在用户列表上运行脚本,请注释第 14 行并添加此代码。

$users = ForEach ($mguser in $(get-content -path C:\it\users.txt)) {
get-mguser -userid $mguser
}

[玩转系统] 使用 PowerShell 获取 MFA 状态(包含脚本)

然后创建一个包含用户列表的文本文件。您可以将列表保存在任何您想要的位置,只需确保更新脚本中的路径即可。

[玩转系统] 使用 PowerShell 获取 MFA 状态(包含脚本)

现在运行该脚本,它将仅处理文本文件中列出的帐户。

使用 Azure GUI 工具包的 MFA 状态报告

我正在开发一个图形工具,其中包括多个报告和用于管理 Azure 和 Office 365 的工具。您只需单击一个按钮即可生成 MFA 报告。

如果您想成为该工具的 Beta 测试人员,请参阅联系页面。

[玩转系统] 使用 PowerShell 获取 MFA 状态(包含脚本)

MFA 身份验证方法列表

以下是 PowerShell 脚本检查的身份验证方法的列表。

  • emailAuthenticationMethod - 表示用户注册的电子邮件地址
  • fido2AuthenticationMethod - 注册到用户(USB 设备)的 FIDO2 安全密钥
  • microsoftAuthenticatorAuthenticationMethod - 这是 Microsoft 身份验证器应用程序。
  • passwordAuthenticationMethod - 用户的密码。
  • phoneAuthenticationMethod - 这意味着用户已使用短信或语音呼叫注册其电话。
  • softwareOathAuthenticationMethod - 注册给用户的软件 OATH 令牌。
  • 临时访问密码验证方法 - 临时限时密码。
  • windowsHelloForBusinessAuthenticationMethod - 向用户注册的 Windows hello for Business。

在脚本中,它们的名称中将包含 #microsoft.graph

[玩转系统] 使用 PowerShell 获取 MFA 状态(包含脚本)

要了解有关身份验证方法的更多信息,请参阅 Microsoft 文章authenticationMethods 资源类型。

我希望这篇文章对您有用,如果您有意见或问题请在下面发表。

相关文章:

  • 如何将 Active Directory 用户导出到 CSV
  • PowerShell 获取组成员
  • 获取 Azure AD 用户 PowerShell

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯