Monitoring failed sign-in attempts is crucial for maintaining the security of your Microsoft 365 environment. By tracking failed logins, administrators can identify potential security threats and take necessary actions to protect their organization. In this article, we will guide you through a PowerShell script that leverages Microsoft Graph to track failed user sign-ins and explain its components in detail.
Here's a PowerShell script that connects to Microsoft Graph, retrieves sign-in logs for a specified period, and identifies users with failed sign-ins:
# Connect to Microsoft Graph
Connect-MgGraph -Scopes "AuditLog.Read.All"
# Define the time range for sign-in logs (e.g. last 7 days)
$startTime = (Get-Date).AddDays(-7).ToString("yyyy-MM-ddTHH:mm:ssZ")
$endTime = (Get-Date).ToString("yyyy-MM-ddTHH:mm:ssZ")
# Get sign-in logs for the specified time range
$signInLogs = Get-MgAuditLogSignIn -Filter "createdDateTime ge $startTime and createdDateTime le $endTime"
# Initialize an array to hold users with failed sign-ins
$failedSignInUsers = @()
# Loop through each sign-in log and check for failed sign-ins
foreach ($log in $signInLogs) {
if ($log.Status.ErrorCode -ne 0) {
$failedSignInUsers += [PSCustomObject]@{
UserId = $log.UserId
UserPrincipalName = $log.UserPrincipalName
SignInStatus = $log.Status.ErrorCode
FailureReason = $log.Status.FailureReason
Timestamp = $log.CreatedDateTime
}
}
}
# Output the list of users with failed sign-ins
$failedSignInUsers | Format-Table -AutoSize
Script Output
The script begins by establishing a connection to Microsoft Graph using the Connect-MgGraph cmdlet with the AuditLog.Read.All scope, which is necessary to read audit logs.
Connect-MgGraph -Scopes "AuditLog.Read.All"
The script defines a time range for the sign-in logs. In this example, we retrieve logs from the past 7 days.
# Define the time range for sign-in logs (e.g. last 7 days)
$startTime = (Get-Date).AddDays(-7).ToString("yyyy-MM-ddTHH:mm:ssZ")
$endTime = (Get-Date).ToString("yyyy-MM-ddTHH:mm:ssZ")
Using the Get-MgAuditLogSignIn cmdlet, the script fetches the sign-in logs within the specified time range.
# Get sign-in logs for the specified time range
$signInLogs = Get-MgAuditLogSignIn -Filter "createdDateTime ge $startTime and createdDateTime le $endTime"
The script loops through each log entry and checks if the ErrorCode is not 0, which indicates a failed sign-in attempt.
# Loop through each sign-in log and check for failed sign-ins
foreach ($log in $signInLogs) {
if ($log.Status.ErrorCode -ne 0) {
$failedSignInUsers += [PSCustomObject]@{
UserId = $log.UserId
UserPrincipalName = $log.UserPrincipalName
SignInStatus = $log.Status.ErrorCode
FailureReason = $log.Status.FailureReason
Timestamp = $log.CreatedDateTime
}
}
}
Users with failed sign-ins are stored in an array and then outputted in a tabular format using Format-Table.
# Output the list of users with failed sign-ins
$failedSignInUsers | Format-Table -AutoSize
Error: Get-MgAuditLogSignIn : Request Authorization failed. Status: 403 (Forbidden) ErrorCode: accessDenied
Solution: Ensure your account has the AuditLog.Read.All permission and that admin consent has been granted.
Error: Get-MgAuditLogSignIn : The specified time range is invalid
Solution: Verify the time range format and ensure the start time is before the end time.
Error: The script runs successfully but returns no data.
Solution: Ensure there are sign-in activities within the specified time range and that you have the correct scopes and permissions.
Tracking failed user sign-ins using Microsoft Graph PowerShell is a powerful way to enhance your organization's security posture. By monitoring and responding to failed login attempts, administrators can proactively address potential security threats and ensure compliance with security policies. This script provides a solid foundation for identifying failed sign-ins and can be further customized to meet your specific requirements.
© m365corner.com. All Rights Reserved. Design by HTML Codex