/
Get-MgEntraMFAStatus.ps1
247 lines (209 loc) · 7.36 KB
/
Get-MgEntraMFAStatus.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<#
.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://lazyadmin.nl
#>
[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
}
}
Write-Verbose "Retrieved $($users.count) users"
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) {
write-verbose "Skipping $($reportUser.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
}