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

[玩转系统] 如何从 Microsoft Entra 获得 MFA 状态

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

如何从 Microsoft Entra 获得 MFA 状态


当您启用安全默认值或启用每用户 MFA 时,您有时需要检查他们是否为多重身份验证配置了以及配置了什么。他们使用哪些方法,是否可以使用无密码登录等。

现在可以在 Microsoft Entra 中查看为用户配置的 MFA 方法。我们还可以查看哪些方法最常用,当然还有哪些用户尚未配置 MFA。

在本文中,我们将了解 Microsoft Entra 中的内置报告功能,并向您展示如何使用 PowerShell 轻松将信息导出到 Excel 文件。

微软 Entra MFA 报告

Microsoft 正在从每用户 MFA 配置转向条件访问策略。这样,您就可以更好地控制用户何时需要使用 MFA 登录、何时不需要。但要配置条件访问策略,您需要拥有 Azure P1 或 P2 许可证。

笔记

要使用本文中的功能,您需要拥有 Azure P1 或 P2 许可证,没有它,您将无法查看 MFA 报告或使用 PowerShell 导出数据。

查看本文反而

因此,在启用条件访问策略之前,最好检查用户当前使用的 MFA 方法。您可能希望采用无密码身份验证,但要做到这一点,您的用户需要配置正确的方法。

要查看 Microsoft Entra 中注册的方法,您需要导航到“身份验证方法”报告:

  1. 打开 Microsoft Entra 管理中心

  2. 展开保护

  3. 打开身份验证方法

  4. 点击用户注册详细信息

[玩转系统] 如何从 Microsoft Entra 获得 MFA 状态

例如,您可以使用页面顶部的筛选选项来查看没有 MFA 的用户或未配置自助密码重置 (SSPR) 的用户。

“活动”页面将为您提供一些最常用方法的概述,以及一些没有 MFA 的用户百分比的统计数据。

使用 PowerShell 获取 Entra MFA 状态

我之前创建过 PowerShell 脚本,用于使用 PowerShell 获取用户的 MFA 状态。如果您没有 Azure P1 或 P2 许可证,则可以使用此脚本获取状态。

本文中的脚本将使用 Microsoft Graph 从 Microsoft Entra 获取 MFA 状态。此报告的优点是它将获取默认方法、显示所有已注册的方法以及用户是否具有 MFA 和无密码功能。

笔记

要获得默认方法,您需要使用 beta 模块。确保您还安装了测试版模块:

Install-Module Microsoft.Graph.Beta -AllowClobber -Force

要获取与 Microsoft Entra 中看到的完全相同的数据,您只需使用 cmdlet Get-MgReportAuthenticationMethodUserRegistrationDetail 即可。值得一提的是,此 cmdlet 将仅返回已启用的帐户。

您需要安装 Microsoft Graph PowerShell 模块并使用 Reports.Read.All 范围连接到 Graph:

# Connect to Graph
Connect-MgGraph -Scopes "Reports.Read.All"

# Get all report data
# Note, we are using the beta module here, so we can get the default method as well
Get-MgBetaReportAuthenticationMethodUserRegistrationDetail

完整脚本

我创建了一个脚本,使您可以更好地控制要包含在导出中的用户。例如,我经常只想检查授权用户,或者快速验证管理员的 MFA 方法。

您可以使用以下参数来过滤结果:

  • 仅限管理员 - $true 或 $false。将仅导出具有管理员角色的用户

  • 许可 - 对、错、两者。仅包括许可用户、未许可用户或所有用户。

  • withOutMFAOnly - $true 或 $false。仅导出没有 MFA 的用户

  • UserPrincipalName - <string>。允许您指定要检索的用户主体名称

该脚本将首先获取所有报告数据,然后过滤掉请求的用户。从技术上讲,也可以只获取选定用户的报告数据,但事实证明这比先获取所有数据要慢得多。

为了获取用户,我们将向 Microsoft Graph 发出单独的请求,以获取应用了筛选器的用户。如果您查看脚本中的 Get-Users 函数,您将看到一个 Select 数组。这些是包含在导出中的属性。您可以根据需要扩展此列表,例如,获取部门信息或用户的职位。

[玩转系统] 如何从 Microsoft Entra 获得 MFA 状态

该脚本会将以下数据导出到 CSV 文件:

  • 显示名称

  • 电子邮件地址

  • 用户主体名称

  • 用户类型(会员或访客)

  • 是管理员

  • 支持 MFA(如果为 true,则用户已配置 MFA)

  • MFA 默认方法

  • MFA 辅助方法

  • MFA 注册方法

  • 无密码能力

  • SSPR 注册

  • 具有 SSPR 能力

  • 用户管理员

您可以从我的 Github 存储库获取该脚本的最新版本。要运行该脚本,请将文件保存到工作目录并在 PowerShell 中使用可选参数运行该脚本。在本文中阅读有关运行 PowerShell 脚本的更多信息。

<#
.Synopsis
  Get the MFA status for all users, admin or selected users from Microsoft Entra

.DESCRIPTION
  This script will get the Azure MFA Status for your users. You can query all the users, admins only or a single user.
   
	It will return the MFA Status, MFA type, Default method, SSRP, and registered devices.

.NOTES
  Name: Get-MgEntraMFAStatus
  Author: R. Mens - LazyAdmin.nl
  Version: 1.2
  DateCreated: Feb 2024
  Purpose/Change: Fixed licensed and enabled users

.LINK
  https://a-d.site
#>
 
[CmdletBinding(DefaultParameterSetName="Default")]
param(
  [Parameter(
    Mandatory = $false,
    ParameterSetName  = "UserPrincipalName",
    HelpMessage = "Enter a single UserPrincipalName or a comma separted list of UserPrincipalNames",
    Position = 0
    )]
  [string[]]$UserPrincipalName,

  [Parameter(
    Mandatory = $false,
    ValueFromPipeline = $false,
    ParameterSetName  = "AdminsOnly"
  )]
  # Get only the users that are an admin
  [switch]$adminsOnly = $false,

  [Parameter(
    Mandatory         = $false,
    ValueFromPipeline = $false,
    ParameterSetName  = "allUsers"
  )]
  [Alias("Licensed")]
  [ValidateSet("true", "false", "both")]
  # Check only the MFA status of users that have license
  [string]$IsLicensed = 'true',

  [Parameter(
    Mandatory         = $false,
    ValueFromPipeline = $true,
    ValueFromPipelineByPropertyName = $true,
    ParameterSetName  = "allUsers"
  )]
  # Get only the users that don't have MFA enabled
  [switch]$withOutMFAOnly = $false,

  [Parameter(
    Mandatory = $false,
    HelpMessage = "Enter path to save the CSV file"
  )]
  [string]$CSVPath = ".\EntraMFAStatus-$((Get-Date -format "MMM-dd-yyyy").ToString()).csv"
)

Function ConnectTo-MgGraph {
  # Check if MS Graph module is installed
  if (-not(Get-InstalledModule Microsoft.Graph)) { 
    Write-Host "Microsoft Graph module not found" -ForegroundColor Black -BackgroundColor Yellow
    $install = Read-Host "Do you want to install the Microsoft Graph Module?"

    if ($install -match "[yY]") {
      Install-Module Microsoft.Graph -Repository PSGallery -Scope CurrentUser -AllowClobber -Force
    }else{
      Write-Host "Microsoft Graph module is required." -ForegroundColor Black -BackgroundColor Yellow
      exit
    } 
  }

  # Connect to Graph
  Write-Host "Connecting to Microsoft Graph" -ForegroundColor Cyan
  Connect-MgGraph -Scopes "Reports.Read.All" -NoWelcome
}


Function Get-Admins{
  <#
  .SYNOPSIS
    Get all user with an Admin role
  #>
  process{
    $admins = Get-MgDirectoryRole | Select-Object DisplayName, Id | 
                %{$role = $_.DisplayName; Get-MgDirectoryRoleMember -DirectoryRoleId $_.id | 
                  where {$_.AdditionalProperties."@odata.type" -eq "#microsoft.graph.user"} | 
                  % {Get-MgUser -userid $_.id }
                } | 
                Select @{Name="Role"; Expression = {$role}}, DisplayName, UserPrincipalName, Mail, Id | Sort-Object -Property Mail -Unique
    
    return $admins
  }
}

Function Get-Users {
  # Set the properties to retrieve
  $select = @(
    'mail'
    'userprincipalname'
    'displayname'
  )

  $properties = $select + "AssignedLicenses"
  
  # Get-MgBetaReportAuthenticationMethodUserRegistrationDetail returns only enabled accounts
  # so we get only enabled accounts here as well.
  $filter = "AccountEnabled eq true and UserType eq 'member'"

  # Check if UserPrincipalName(s) are given
  if ($UserPrincipalName) {
    Write-host "Get specific users" -ForegroundColor Cyan

    $users = @()
    foreach ($user in $UserPrincipalName) 
    {
      try {
        $users += Get-MgUser -UserId $user -Property $properties | select $select -ErrorAction Stop
      }
      catch {
        [PSCustomObject]@{
          DisplayName       = " - Not found"
          UserPrincipalName = $User
          isAdmin           = $null
          MFAEnabled        = $null
        }
      }
    }
  }elseif($adminsOnly) {
    Write-host "Get admins only" -ForegroundColor Cyan

    $users = @()
    foreach ($admin in $admins) {
      $users += Get-MgUser -UserId $admin.UserPrincipalName -Property $properties | select $select
    }
  }else{
    if ($IsLicensed -eq 'true') {
      # Get only licensed users
      $users = Get-MgUser -Filter $filter -Property $properties -all | Where-Object {($_.AssignedLicenses).count -gt 0} | select $select
    }elseif ($IsLicensed -eq 'false') {
      # Get only users without license
      $users = Get-MgUser -Filter $filter -Property $properties -all | Where-Object {($_.AssignedLicenses).count -eq 0} | select $select
    }else{
      $users = Get-MgUser -Filter $filter -Property $properties -all | select $select
    }
  }
  return $users
}

Function Get-Manager {
  <#
    .SYNOPSIS
      Get the manager users
  #>
  param(
    [Parameter(Mandatory = $true)] $userId
  )
  process {
    $manager = Get-MgUser -UserId $userId -ExpandProperty manager | Select @{Name = 'name'; Expression = {$_.Manager.AdditionalProperties.displayName}}
    return $manager.name
  }
}

Function Get-MFAStatusUsers {
  <#
    .SYNOPSIS
      Get all AD users
  #>
  process {
    Write-Host "Collecting users" -ForegroundColor Cyan
    
    # Collect users to get
    $users = Get-Users
    
    # Get all MFA Report data
    $reportData = Get-MgBetaReportAuthenticationMethodUserRegistrationDetail

    Write-Host "Processing" $users.count "users" -ForegroundColor Cyan

    # Collect and loop through all users
    foreach ($reportUser in $reportData) {

      # Check if we want this users in the output
      # Note - it's faster to get all report data and then filter it on the selected users, 
      # then getting the report data for each individual user
      if ($reportUser.UserPrincipalName -notin $users.UserPrincipalName) {
        continue
      }

      Write-Host "- Processing $($reportUser.UserDisplayName)" -ForegroundColor Cyan
    
      # Get the user data from the users array
      $userData = $users | where-object {$_.UserPrincipalName -eq $reportUser.UserPrincipalName}

      # Get the manager of the user (optional)
      $manager = Get-Manager -userId $reportUser.id
      
      # Create output object
      [pscustomobject]@{
        "Name" = $userData.DisplayName
        "Emailaddress" = $userData.mail
        "UserPrincipalName" = $reportUser.UserPrincipalName
        "User Type" = $reportUser.UserType
        "isAdmin" = $reportUser.IsAdmin
        "MFA Capable" = $reportUser.IsMfaCapable
        "MFA Default method" = $reportUser.DefaultMfaMethod
        "MFA Secondary method" = $reportUser.UserPreferredMethodForSecondaryAuthentication
        "MFA Methods Registered" = $reportUser.MethodsRegistered -join ", "
        "Passwordless Capable" = $reportUser.IsPasswordlessCapable
        "SSPR Registered" = $reportUser.IsSsprRegistered
        "SSPR Capable" = $reportUser.IsSsprCapable
        "Manager" = $manager
      }
    }
  }
}

# Connect to Graph
ConnectTo-MgGraph

# Get Admins
# Get all users with admin role
$admins = $null

if ($adminsOnly) {
  $admins = Get-Admins
} 

# Get MFA Status
Get-MFAStatusUsers | Sort-Object Name | Export-CSV -Path $CSVPath -NoTypeInformation

if ((Get-Item $CSVPath).Length -gt 0) {
  Write-Host "Report finished and saved in $CSVPath" -ForegroundColor Green

  # Open the CSV file
  Invoke-Item $CSVPath
}else{
  Write-Host "Failed to create report" -ForegroundColor Red
}

包起来

跟踪用户的 MFA 配置对于确保 Microsoft 3675 租户的安全非常重要。 Microsoft Entra 中的新报告使您可以轻松跟踪那些未完成 MFA 设置过程或正在使用过时的 MFA 方法的用户。

尽管 Microsoft Entra 中的报告提供了最需要的信息,但使用 PowerShell,我们可以包含联系方式详细信息、用户经理或部门详细信息,这些信息非常有帮助。

我希望您喜欢这篇文章和脚本,如果您有任何疑问,请在下面发表评论。

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

取消回复欢迎 发表评论:

关灯