דלג לתוכן

אימות מספרי טלפון של משתמשים מול הגדרות MFA באמצעות Microsoft Graph PowerShell

הבטחה שמספרי הטלפון של משתמשים מוגדרים בצורה מדויקת הן בפרופיל שלהם והן בהגדרות אימות רב-גורמי (MFA) היא קריטית לשמירה על אבטחה חזקה בארגון. מאמר זה מפרט כיצד להשתמש בסקריפט PowerShell המנצל את Microsoft Graph API כדי לאמת שמספרי הטלפון הראשיים של משתמשים תואמים למספרי הטלפון של ה-MFA שלהם.

הסקריפט מאוטמט את תהליך השוואת מספרי הטלפון, בודק אי-התאמות שעלולות להוביל לבעיות אימות או סיכוני אבטחה. זה יכול להיות שימושי במיוחד למנהלי IT שצריכים לבקר ולנקות תצורות מספרי טלפון של משתמשים ברחבי הארגון.

חלון טרמינל
# Install the Microsoft Graph PowerShell SDK (if not already installed)
if (-not (Get-Module -ListAvailable -Name "Microsoft.Graph")) {
Install-Module Microsoft.Graph -Scope CurrentUser -Force
}
# Connect to Microsoft Graph
Connect-MgGraph -Scopes "User.Read.All", "UserAuthenticationMethod.Read.All"
# Function to get user phone number
function Get-UserPhoneNumber {
param (
[string]$userId
)
$user = Get-MgUser -UserId $userId
return $user.MobilePhone
}
# Function to get MFA phone numbers
function Get-MfaPhoneNumbers {
param (
[string]$userId
)
# Get all phone authentication methods for the user
$phoneMethods = Get-MgUserAuthenticationPhoneMethod -UserId $userId
return $phoneMethods
}
# Function to clean phone numbers (remove hyphens)
function Clean-PhoneNumber {
param (
[string]$phoneNumber
)
return $phoneNumber -replace '-', ''
}
# Function to check if at least 6 digits match
function Check-PhoneNumberMatch {
param (
[string]$primaryNumber,
[string[]]$mfaNumbers
)
$primaryDigits = ($primaryNumber -replace '\D', '') # Remove all non-digit characters
foreach ($mfaNumber in $mfaNumbers) {
$mfaDigits = ($mfaNumber -replace '\D', '') # Remove all non-digit characters
$matchCount = ($primaryDigits.ToCharArray() | Where-Object { $mfaDigits.Contains($_) }).Count
if ($matchCount -ge 6) {
return $true
}
}
return $false
}
# Get a list of users (excluding guest users)
$users = Get-MgUser -Filter "accountEnabled eq true and userType eq 'Member'"
# Iterate over each user and compare phone numbers
foreach ($user in $users) {
$userId = $user.Id
$userPhoneNumber = Get-UserPhoneNumber -userId $userId
$mfaPhoneNumbers = Get-MfaPhoneNumbers -userId $userId
if (-not [string]::IsNullOrWhiteSpace($userPhoneNumber)) {
$cleanUserPhoneNumber = Clean-PhoneNumber -phoneNumber $userPhoneNumber
$cleanMfaPhoneNumbers = @()
if ($mfaPhoneNumbers) {
foreach ($mfaPhone in $mfaPhoneNumbers) {
$cleanMfaPhoneNumbers += Clean-PhoneNumber -phoneNumber $mfaPhone.PhoneNumber
}
$isMatching = Check-PhoneNumberMatch -primaryNumber $cleanUserPhoneNumber -mfaNumbers $cleanMfaPhoneNumbers
Write-Output "User: $($user.DisplayName)"
Write-Output "User Phone Number: $cleanUserPhoneNumber"
if ($isMatching) {
Write-Output "At least 6 digits match with MFA phone numbers."
} else {
Write-Output "No match with MFA phone numbers."
}
} else {
Write-Output "No MFA phone numbers found."
}
Write-Output "-------------------------------------------"
} else {
Write-Output "User: $($user.DisplayName) - No primary phone number found."
}
}