Skip to content

Commit f97cb87

Browse files
Merge pull request KelvinTegelaar#1778 from criani/dev
Added null protection to ListMFAUsers.ps1 supporting fallback when calling non P1 tenants
2 parents 7d4f249 + d96bb4a commit f97cb87

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Identity/Reports/Invoke-ListMFAUsers.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,9 @@ function Invoke-ListMFAUsers {
7373
}
7474

7575
return ([HttpResponseContext]@{
76-
StatusCode = [HttpStatusCode]::OK
76+
StatusCode = $StatusCode
7777
Body = @($GraphRequest)
7878
})
7979

80+
8081
}

Modules/CIPPCore/Public/Get-CIPPMFAState.ps1

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,31 @@ function Get-CIPPMFAState {
1818
}
1919

2020
$Errors = [System.Collections.Generic.List[object]]::new()
21+
$SecureDefaultsState = $null
22+
$CASuccess = $false
23+
$CAError = $null
24+
$PolicyTable = @{}
25+
$AllUserPolicies = @()
26+
$UserGroupMembership = @{}
27+
$UserExcludeGroupMembership = @{}
28+
$GroupNameLookup = @{}
29+
$MFAIndex = @{}
30+
2131
try {
2232
$SecureDefaultsState = (New-GraphGetRequest -Uri 'https://graph.microsoft.com/beta/policies/identitySecurityDefaultsEnforcementPolicy' -tenantid $TenantFilter ).IsEnabled
2333
} catch {
2434
Write-Host "Secure Defaults not available: $($_.Exception.Message)"
2535
$Errors.Add(@{Step = 'SecureDefaults'; Message = $_.Exception.Message })
36+
$SecureDefaultsState = $null
2637
}
2738
$CAState = [System.Collections.Generic.List[object]]::new()
2839

2940
try {
30-
$MFARegistration = (New-GraphGetRequest -uri "https://graph.microsoft.com/beta/reports/authenticationMethods/userRegistrationDetails?$top=999&$select=userPrincipalName,isMfaRegistered,isMfaCapable,methodsRegistered" -tenantid $TenantFilter -asapp $true)
31-
$MFAIndex = @{}
41+
$MFARegistration = (New-GraphGetRequest -uri "https://graph.microsoft.com/beta/reports/authenticationMethods/userRegistrationDetails?`$top=999&`$select=userPrincipalName,isMfaRegistered,isMfaCapable,methodsRegistered" -tenantid $TenantFilter -asapp $true)
3242
foreach ($MFAEntry in $MFARegistration) {
33-
$MFAIndex[$MFAEntry.userPrincipalName] = $MFAEntry
43+
if ($null -ne $MFAEntry.userPrincipalName) {
44+
$MFAIndex[$MFAEntry.userPrincipalName] = $MFAEntry
45+
}
3446
}
3547
} catch {
3648
$CAState.Add('Not Licensed for Conditional Access') | Out-Null
@@ -39,12 +51,11 @@ function Get-CIPPMFAState {
3951
$Errors.Add(@{Step = 'MFARegistration'; Message = $_.Exception.Message })
4052
}
4153
Write-Host "User registration details not available: $($_.Exception.Message)"
42-
$MFAIndex = @{}
4354
}
4455

4556
if ($null -ne $MFARegistration) {
46-
$CASuccess = $true
4757
try {
58+
$CASuccess = $true
4859
$CAPolicies = (New-GraphGetRequest -Uri 'https://graph.microsoft.com/beta/identity/conditionalAccess/policies?$top=999&$filter=state eq ''enabled''&$select=id,displayName,state,grantControls,conditions' -tenantid $TenantFilter -ErrorAction Stop -AsApp $true)
4960
$PolicyTable = @{}
5061
$AllUserPolicies = [System.Collections.Generic.List[object]]::new()
@@ -315,11 +326,7 @@ function Get-CIPPMFAState {
315326

316327
$PerUser = $_.PerUserMFAState
317328

318-
$MFARegUser = if ($null -eq ($MFAIndex[$_.UserPrincipalName])) {
319-
$false
320-
} else {
321-
$MFAIndex[$_.UserPrincipalName]
322-
}
329+
$MFARegUser = $MFAIndex[$_.UserPrincipalName]
323330

324331
[PSCustomObject]@{
325332
Tenant = $TenantFilter
@@ -329,9 +336,9 @@ function Get-CIPPMFAState {
329336
AccountEnabled = $_.accountEnabled
330337
PerUser = $PerUser
331338
isLicensed = $_.isLicensed
332-
MFARegistration = if ($MFARegUser) { $MFARegUser.isMfaRegistered } else { $false }
333-
MFACapable = if ($MFARegUser) { $MFARegUser.isMfaCapable } else { $false }
334-
MFAMethods = if ($MFARegUser) { $MFARegUser.methodsRegistered } else { @() }
339+
MFARegistration = if ($null -ne $MFARegUser) { [bool]$MFARegUser.isMfaRegistered } else { $null }
340+
MFACapable = if ($null -ne $MFARegUser) { [bool]$MFARegUser.isMfaCapable } else { $null }
341+
MFAMethods = if ($null -ne $MFARegUser) { @($MFARegUser.methodsRegistered) } else { @() }
335342
CoveredByCA = $CoveredByCA
336343
CAPolicies = $UserCAState
337344
CoveredBySD = $SecureDefaultsState

0 commit comments

Comments
 (0)