|
| 1 | +function Remove-CIPPUserMFA { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Remove MFA methods for a user |
| 5 | +
|
| 6 | + .DESCRIPTION |
| 7 | + Remove MFA methods for a user using bulk requests to the Microsoft Graph API |
| 8 | +
|
| 9 | + .PARAMETER UserPrincipalName |
| 10 | + UserPrincipalName of the user to remove MFA methods for |
| 11 | +
|
| 12 | + .PARAMETER TenantFilter |
| 13 | + Tenant where the user resides |
| 14 | +
|
| 15 | + .EXAMPLE |
| 16 | + Remove-CIPPUserMFA -UserPrincipalName testuser@contoso.com -TenantFilter contoso.com |
| 17 | +
|
| 18 | + #> |
| 19 | + [CmdletBinding(SupportsShouldProcess = $true)] |
| 20 | + Param( |
| 21 | + [Parameter(Mandatory = $true)] |
| 22 | + [string]$UserPrincipalName, |
| 23 | + [Parameter(Mandatory = $true)] |
| 24 | + [string]$TenantFilter, |
| 25 | + [Parameter(Mandatory = $false)] |
| 26 | + [string]$ExecutingUser = 'CIPP' |
| 27 | + ) |
| 28 | + |
| 29 | + Write-Information "Getting auth methods for $UserPrincipalName" |
| 30 | + $AuthMethods = New-GraphGetRequest -uri "https://graph.microsoft.com/v1.0/users/$UserPrincipalName/authentication/methods" -tenantid $TenantFilter -AsApp $true |
| 31 | + $Requests = [System.Collections.Generic.List[object]]::new() |
| 32 | + foreach ($Method in $AuthMethods) { |
| 33 | + if ($Method.'@odata.type' -and $Method.'@odata.type' -ne '#microsoft.graph.passwordAuthenticationMethod') { |
| 34 | + $MethodType = ($Method.'@odata.type' -split '\.')[-1] -replace 'Authentication', '' |
| 35 | + $Requests.Add(@{ |
| 36 | + id = "$MethodType-$($Method.id)" |
| 37 | + method = 'DELETE' |
| 38 | + url = ('users/{0}/authentication/{1}s/{2}' -f $UserPrincipalName, $MethodType, $Method.id) |
| 39 | + }) |
| 40 | + } |
| 41 | + } |
| 42 | + if (($Requests | Measure-Object).Count -eq 0) { |
| 43 | + Write-LogMessage -API 'Remove-CIPPUserMFA' -tenant $TenantFilter -message "No MFA methods found for user $UserPrincipalName" -sev 'Info' |
| 44 | + $Results = [pscustomobject]@{'Results' = "No MFA methods found for user $($Request.Query.ID)" } |
| 45 | + Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{ |
| 46 | + StatusCode = [HttpStatusCode]::OK |
| 47 | + Body = $Results |
| 48 | + }) |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + if ($PSCmdlet.ShouldProcess("Remove MFA methods for $UserPrincipalName")) { |
| 53 | + $Results = New-GraphBulkRequest -Requests $Requests -tenantid $TenantFilter -asapp $true -erroraction stop |
| 54 | + if ($Results.status -eq 204) { |
| 55 | + Write-LogMessage -API 'Remove-CIPPUserMFA' -tenant $TenantFilter -message "Successfully removed MFA methods for user $UserPrincipalName" -sev 'Info' |
| 56 | + $Results = [pscustomobject]@{'Results' = "Successfully completed request. User $($Request.Query.ID) must supply MFA at next logon" } |
| 57 | + } else { |
| 58 | + $FailedAuthMethods = (($Results | Where-Object { $_.status -ne 204 }).id -split '-')[0] -join ', ' |
| 59 | + Write-LogMessage -API 'Remove-CIPPUserMFA' -tenant $TenantFilter -message "Failed to remove MFA methods for $FailedAuthMethods" -sev 'Error' |
| 60 | + $Results = [pscustomobject]@{'Results' = "Failed to reset MFA methods for $FailedAuthMethods" } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + return $Results |
| 65 | +} |
0 commit comments