首页 > 其他分享 >Azure脚本分享,批量导出用户MFA状态

Azure脚本分享,批量导出用户MFA状态

时间:2022-12-19 11:02:31浏览次数:58  
标签:MFA 导出 UserPrincipalName Write _. Azure Output true

一年时间又快过去了,又攒了一些内容可以拿出来分享,希望能帮到有需要的人,先从一些比较简单的开始吧,最近有个需求是想看到用户是不是都设置了MFA,设置了MFA代表相对来说比较安全,所以全面了解下组织内部现在MFA的状态还是有必要的

另外再来普及个基本概念,在Azure里实现MFA有两种方式

  1. Per User MFA
  1. 免费的,不需要额外license
  2. 没办法设置详细的策略,比如针对哪一个Azure服务设置MFA,以及MFA的具体策略等等
  1. Conditional Access
  1. 需要最低AAD P1的license
  2. 可以针对各种场景设置很详细的策略,比如什么场景下需要MFA,哪些场景下不需要等

有时间的话可以再详细介绍下,暂时先回到主题,如何批量导出用户MFA状态

能想到比较快的方法还是通过脚本,搜了下之前已经有大神有类似脚本了,比如下边这位大神

​https://techcommunity.microsoft.com/t5/itops-talk-blog/powershell-basics-how-to-check-if-mfa-is-enabled-in-azure-and/ba-p/1486045​

不过有个问题是看了看大神脚本里的逻辑,感觉还不能完全满足需求,所以在大神的脚本基础上,稍微改了改,算是借花献佛,脚本内容现在分享出来

[CmdletBinding()]
param(
[Parameter(
Position = 0,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true
)]

[string[]] $UserPrincipalName ,
[int] $MaxResults = 10000
)

BEGIN {
$AdminUsers = Get-MsolRoleMember -RoleObjectId '62e90394-69f5-4237-9190-012177145e10' -ErrorAction Stop | Select EmailAddress -Unique

}

PROCESS {
if ($UserPrincipalName) {
`
foreach ($User in $UserPrincipalName) {
try {
Get-MsolUser -UserPrincipalName $User -ErrorAction Stop | select DisplayName, UserPrincipalName, IsLicensed, `
@{Name = 'isCompanyAdmin'; Expression = { if ($AdminUsers -match $_.UserPrincipalName) { Write-Output $true } else { Write-Output $false } } }, `
@{Name = 'MFAEnabled'; Expression = { if ($_.StrongAuthenticationRequirements.State -eq "Enabled") { Write-Output $true } elseif ($null -ne $_.StrongAuthenticationMethods.MethodType) {
Write-Output $true
}
else { Write-Output $false } }
}



}
catch {

$Object = [pscustomobject]@{
DisplayName = '_NotSynced'
UserPrincipalName = $User
isAdmin = '-'
MFAEnabled = '-'
}
Write-Output $Object
}
}
}
else {
$AllUsers = Get-MsolUser -MaxResults $MaxResults | select DisplayName, UserPrincipalName, LastDirSyncTime, LastPasswordChangeTimestamp, ObjectId, ValidationStatus , WhenCreated, IsLicensed, `
@{Name = 'isCompanyAdmin'; Expression = { if ($AdminUsers -match $_.UserPrincipalName) { Write-Output $true } else { Write-Output $false } } }, `
@{Name = 'MFAEnabled'; Expression = { if ($_.StrongAuthenticationRequirements.State -eq "Enabled") { Write-Output $true } elseif ($null -ne $_.StrongAuthenticationMethods.MethodType) {
Write-Output $true
}
else { Write-Output $false } }
}
}
}
END {

if ($null -ne $AllUsers) {
$path = [Environment]::GetFolderPath("Desktop") + "\" + "ADUsers-" + $(Get-Date -Format "yyyyMMdd-HHmmss") + ".csv"
$AllUsers | Export-Csv -LiteralPath $path -NoTypeInformation -Force
Write-Output "$(get-date) * Please check $path"
}
elseif (!$UserPrincipalName) {
Write-Output "$(get-date) * User information not found."
}
}

基本使用方式和大神之前逻辑是一样的,可以导出一部分用户的信息,也可以一次性把所有用户信息全部导出来,但是判断MFA的逻辑以及admin等逻辑和大神的还是有一点区别,最后如果是导出全部用户信息,会生成一个csv文件,以便检查

Azure脚本分享,批量导出用户MFA状态_MFA

标签:MFA,导出,UserPrincipalName,Write,_.,Azure,Output,true
From: https://blog.51cto.com/mxyit/5951539

相关文章