Skip to content

Instantly share code, notes, and snippets.

@machv
Created October 20, 2021 14:50
Show Gist options
  • Save machv/7d4ca1ae7ef122e30babf443ab6f3f04 to your computer and use it in GitHub Desktop.
Save machv/7d4ca1ae7ef122e30babf443ab6f3f04 to your computer and use it in GitHub Desktop.
$configFilePath = "$($env:USERPROFILE)\aad.cnf"
$data = Get-Content $configFilePath
$config = [System.Text.Encoding]::Unicode.GetString([Convert]::FromBase64String($data)) | ConvertFrom-Json
$token = Invoke-ClientCredentialsFlow -Tenant $config.TenantId -ClientId $config.ClientId -ClientSecret $config.ClientSecret
$headers = @{
"Authorization" = "Bearer $($token.AccessToken)"
}
# Get AAD Users.
$results = Invoke-RestMethod -Headers $headers -Uri "https://graph.microsoft.com/v1.0/users?`$top=999" -Method Get
$aadUsers = $results.value
if ($results.'@odata.nextLink') {
$aadUsers += $results.value
do {
$results = Invoke-RestMethod -Headers $headers -Uri $results.'@odata.nextLink' -Method Get
$aadUsers += $results.value
} while ($results.'@odata.nextLink')
}
$user = $aadUsers | Select -First 1
foreach ($user in $aadUsers) {
$upn = [System.Web.HTTPUtility]::UrlEncode($user.userPrincipalName)
$authMethods = Invoke-RestMethod -Headers $headers -Uri "https://graph.microsoft.com/beta/users/$($upn)/authentication/methods" -Method Get
if ($authMethods.value.count -gt 0) {
$user | Add-Member -Type NoteProperty -Name "authMethods" -Value @($authMethods.value).'@odata.type'.replace("#microsoft.graph.", "")
$authDetails = $authMethods.value
$methods = @()
foreach ($authMethod in $authDetails) {
$methodName = $authMethod.'@odata.type'.replace("#microsoft.graph.", "")
$authMethod.'@odata.type' = $methodName
$methods += $methodName
}
$user | Add-Member -Type NoteProperty -Name "authMethodsDetail" -Value @($authDetails)
$user | Add-Member -Type NoteProperty -Name "authMethodsCount" -Value $authMethods.value.count
$user | Add-Member -Type NoteProperty -Name "authMethodsList" -Value ($methods -join ",")
}
}
$aadUsers | Export-Csv -Path "mfa-report.csv" -NoTypeInformation -Encoding UTF8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment