保护Azure AD安全:Powershellisfun实现MFA手机认证批量配置教程
保护Azure AD安全:Powershellisfun实现MFA手机认证批量配置教程
【免费下载链接】PowershellisfunRepository with the scripts that I have used in my blogs on https://powershellisfun.com. If you like these, please sponsor this project using the Sponsor button below or buy me a coffee :) https://www.buymeacoffee.com/powershellisfun项目地址: https://gitcode.com/gh_mirrors/po/Powershellisfun
在数字化办公环境中,Azure AD的安全防护至关重要。多因素认证(MFA)作为核心安全措施,能有效防范账号盗用风险。本文将介绍如何利用Powershellisfun项目提供的脚本工具,快速实现Azure AD用户MFA手机认证的批量配置,让企业安全防护更高效、更省心!
📋 准备工作:环境与权限配置
在开始配置前,请确保您的环境满足以下条件:
- 已安装PowerShell 7.0或更高版本
- 拥有Azure AD全局管理员或用户管理员权限
- 已安装Microsoft Graph模块(脚本将自动检测并安装缺失模块)
核心脚本文件路径:Adding Mobile Phone Authentication Method For Azure AD MFA/Add_Mobile_Phone_Auth_Method.ps1
🔑 模块安装与连接验证
脚本内置了模块自动检测功能,会检查并安装以下必要模块:
- Microsoft.Graph.Authentication
- Microsoft.Graph.Identity.Signins
- Microsoft.Graph.Users
执行脚本时,系统将通过以下命令连接到Microsoft Graph:
Connect-MgGraph -Scopes UserAuthenticationMethod.ReadWrite.All, User.Read.All -NoWelcome⚠️ 注意:首次运行需要使用全局管理员账号授权,确保已在Azure AD中配置正确的API权限。
📱 批量配置MFA手机认证的核心逻辑
该脚本的工作流程主要分为三个步骤:
- 筛选符合条件的用户:排除来宾账号(
#EXT#),仅处理已配置MFA但缺少手机认证方式的用户 - 检查用户手机信息:自动读取Azure AD中存储的用户
MobilePhone属性 - 批量添加认证方式:对符合条件的用户自动创建手机认证方法
关键代码片段:
foreach ($user in Get-MgBetaUser -All | Where-Object UserPrincipalName -NotMatch '#EXT#') { if ($null -ne (Get-MgBetaUserAuthenticationMethod -UserId $user.UserPrincipalName | Where-Object ID -ne 28c10230-6103-485e-b985-444c60001490) ` -and $null -eq (Get-MgBetaUserAuthenticationPhoneMethod -UserId $user.UserPrincipalName)) { if ($null -ne $user.MobilePhone) { New-MgBetaUserAuthenticationPhoneMethod -UserId $user.UserPrincipalName -phoneType "Mobile" -phoneNumber $user.MobilePhone | Out-Null } } }🚀 执行步骤与结果验证
1. 获取脚本
通过以下命令克隆项目仓库:
git clone https://gitcode.com/gh_mirrors/po/Powershellisfun2. 运行脚本
cd "Adding Mobile Phone Authentication Method For Azure AD MFA" .\Add_Mobile_Phone_Auth_Method.ps13. 查看执行结果
- 🟢 绿色文字:成功为用户添加手机认证方式
- 🔴 红色文字:用户缺少手机号信息,已跳过处理
💡 使用技巧与注意事项
- 批量处理优化:对于超过500用户的租户,建议添加
-PageSize参数分批处理 - 权限最小化原则:推荐使用专用服务账号执行脚本,并仅分配必要的Microsoft Graph权限
- 数据备份:执行前建议导出用户信息:
Get-MgUser -All | Export-Csv -Path "AzureAD_Users_Backup.csv" - 定期审计:配合项目中的
AdminGroupChangeReport.ps1脚本,定期检查MFA配置状态
🔍 常见问题解决
Q: 脚本提示"权限不足"如何处理?
A: 请确保已授予应用程序"UserAuthenticationMethod.ReadWrite.All"和"User.Read.All"权限,可通过Azure AD管理中心的"企业应用"进行配置。
Q: 部分用户添加失败怎么办?
A: 检查用户MobilePhone属性格式是否正确(需包含国家代码,如+14255551234),可通过Set-MgUser -UserId <UPN> -MobilePhone "+14255551234"命令修正。
通过Powershellisfun提供的MFA批量配置工具,企业可以快速提升Azure AD账号的安全性,有效降低账号被盗风险。这个简单而强大的解决方案,让管理员从繁琐的手动配置中解放出来,将更多精力投入到核心安全策略的制定与优化中。立即尝试,为您的组织构建更坚固的身份安全防线吧!
【免费下载链接】PowershellisfunRepository with the scripts that I have used in my blogs on https://powershellisfun.com. If you like these, please sponsor this project using the Sponsor button below or buy me a coffee :) https://www.buymeacoffee.com/powershellisfun项目地址: https://gitcode.com/gh_mirrors/po/Powershellisfun
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
