Report Conditional Access Blocked Sign-in Metrics with PowerShell

  • Post author:
  • Post category:Microsoft Graph
  • Post last modified:
  • Reading time:6 mins read
  • Post published:

Conditional Access Metrics can provide valuable information about how often policies are triggered and their impact on your environment. However, processing large amounts of sign-in logs to deduce this impact can often be cumbersome, depending on the amount of logs your environment is generating.

The getMetricsForConditionalAccessBlockedSignIn endpoint in the Microsoft Graph API can quickly give you insights into how many sign-ins are blocked by policies in general. Sudden jumps in the metrics could indicate an issue that needs addressing. Even if the issue was short-lived and not reported, a deeper investigation into what happened could help prevent it from occurring in the future.

In this article, we will look at how to use this new API to generate data on blocked sign-ins in our environment.

Viewing the blocked sign-in metrics data

The data returned by Microsoft Graph is simple; the intervalStartDateTime is the start time of the defined period where the blocked sign-ins are aggregated, and the value is the number of blocked sign-ins during that period.

For example, if we run the following command, it will return all the blocked sign-ins between the 1st of April 2025 and the 21st of April 2025, aggregated by 30-minute intervals.

$endDate = (Get-Date).ToString("yyyy-MM-ddTHH:mm:ssZ")
$startDate = (Get-Date).AddDays(-30).ToString("yyyy-MM-ddTHH:mm:ssZ")
$interval = "30"

$Uri = "/beta/reports/serviceActivity/getMetricsForConditionalAccessBlockedSignIn(inclusiveIntervalStartDateTime=$startDate,exclusiveIntervalEndDateTime=$endDate,aggregationIntervalInMinutes=$interval)"
$response = Invoke-MgGraphRequest -Method GET -Uri $Uri | Select -Expand value

Here is an example snippet of data returned from the report:

{
    "intervalStartDateTime": "2025-04-01T00:30:00Z",
    "value": 0
},
{
    "intervalStartDateTime": "2025-04-01T01:00:00Z",
    "value": 0
},
{
    "intervalStartDateTime": "2025-04-01T01:30:00Z",
    "value": 0
}

Supported function/search parameters

While this report doesn’t support filters in the traditional sense, parameters in the URI can be included to help refine the data that is returned. The following parameters are supported:

  • inclusiveIntervalStartDateTime: This setting defines the start date of the period from which the data is returned. This cannot be further than 30 days in the past, which is the period for a standard tenant in which sign-in data is stored. This cannot be extended further in the past.
  • exclusiveIntervalEndDateTime: This setting defines the end date of the period from which the data is returned. This has to be later than the start date and before the current date/time.
  • aggregationIntervalInMinutes: This is the interval period in which the blocked sign-in count is aggregated. This can either be 5, 10, 15 or 30 (default is 15). This setting is optional.

While the aggregationIntervalInMinutes property can be defined as a single number, the dates must be in a specific format. Using Get-Date we can obtain the current date, plus or minus some days, to define the start and end date of the period to report on.

$endDate = (Get-Date).ToString("yyyy-MM-ddTHH:mm:ssZ")
$startDate = (Get-Date).AddDays(-30).ToString("yyyy-MM-ddTHH:mm:ssZ")

Visualising the data

Raw data by itself isn’t great for humans to read; in fact, it is almost impossible to get an idea of what is normal and what is abnormal without the data being easy on the eyes.

While I can write PowerShell, I’m not a front-end developer/designer and rely on the likes of GitHub Copilot to help me generate HTML and JavaScript code. Below is an example HTML graph I have created using Copilot to visualise this data, allowing you to see any anomalies in blocked sign-ins. Once investigated, if the cause was within the organisation’s control, whether inadvertent or malicious, action should be taken to prevent it from happening again.

Conditional Access report data
Conditional Access report data

Below is the code which generated the above graph for you to try for yourself!

# Group data by day and calculate the daily totals
$dailyData = $response | Group-Object { $_.intervalStartDateTime.ToString("yyyy-MM-dd") } | ForEach-Object {
    [PSCustomObject]@{
        intervalStartDateTime = [DateTime]::Parse($_.Name)
        Value = ($_.Group | Measure-Object -Property Value -Sum).Sum
    }
}

# Sort by date to ensure proper ordering
$dailyData = $dailyData | Sort-Object intervalStartDateTime

# Create the HTML for the line graph
$htmlContent = @"
<!DOCTYPE html>
<html>
<head>
    <title>Conditional Access Blocked Sign-Ins</title>
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-gradient"></script>
    <style>
        .chart-container {
            width: 900px;
            height: 500px;
            margin: 30px auto;
            padding: 20px;
            border-radius: 12px;
            box-shadow: 0 10px 20px rgba(0,0,0,0.08);
            background-color: white;
        }
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #f9fafc;
            padding: 30px;
            color: #333;
            margin: 0;
        }
        h1 {
            text-align: center;
            color: #443e8b;
            font-weight: 600;
            margin-top: 40px;
            margin-bottom: 10px;
        }
        .subtitle {
            text-align: center;
            color: #666;
            margin-bottom: 30px;
            font-size: 16px;
        }
        .dashboard {
            max-width: 1100px;
            margin: 0 auto;
            padding: 20px;
            border-radius: 15px;
            background: linear-gradient(145deg, #f0f4ff, #ffffff);
        }
    </style>
</head>
<body>
    <div class="dashboard">
        <h1>Conditional Access Blocked Sign-Ins</h1>
        <div class="subtitle">Blocked sign-in data aggregated by day showing activity trends</div>
        <div class="chart-container">
            <canvas id="lineChart"></canvas>
        </div>
    </div>
    <script>
        // Data from PowerShell
        const dates = [$(($dailyData | ForEach-Object { "'$($_.intervalStartDateTime.ToString("yyyy-MM-dd"))'" }) -join ', ')];
        const values = [$(($dailyData | ForEach-Object { $_.Value }) -join ', ')];
        
        // Create the chart
        const ctx = document.getElementById('lineChart').getContext('2d');
        
        const purpleGradient = ctx.createLinearGradient(0, 0, 0, 400);
        purpleGradient.addColorStop(0, 'rgba(116, 90, 242, 0.5)');
        purpleGradient.addColorStop(1, 'rgba(116, 90, 242, 0.0)');
        
        new Chart(ctx, {
            type: 'line',
            data: {
                labels: dates,
                datasets: [{
                    label: 'Daily Values',
                    data: values,
                    borderColor: '#6259ca',
                    backgroundColor: purpleGradient,
                    borderWidth: 3,
                    tension: 0.4,
                    fill: true,
                    pointRadius: 6,
                    pointBackgroundColor: '#4a3bb3',
                    pointBorderColor: 'white',
                    pointBorderWidth: 2,
                    pointHoverRadius: 8,
                    pointHoverBackgroundColor: '#4a3bb3',
                    pointHoverBorderColor: 'white',
                    pointHoverBorderWidth: 2,
                    lineTension: 0.5,
                    cubicInterpolationMode: 'monotone'
                }]
            },
            options: {
                responsive: true,
                maintainAspectRatio: false,
                interaction: {
                    mode: 'index',
                    intersect: false,
                },
                scales: {
                    y: {
                        beginAtZero: true,
                        grid: {
                            drawBorder: false,
                            color: 'rgba(200, 200, 200, 0.15)',
                        },
                        ticks: {
                            font: {
                                family: "'Segoe UI', sans-serif",
                                size: 12
                            },
                            color: '#555'
                        },
                        title: {
                            display: true,
                            text: 'Activity Count',
                            color: '#443e8b',
                            font: {
                                family: "'Segoe UI', sans-serif",
                                size: 14,
                                weight: 'bold'
                            }
                        }
                    },
                    x: {
                        grid: {
                            display: false
                        },
                        ticks: {
                            font: {
                                family: "'Segoe UI', sans-serif",
                                size: 12
                            },
                            color: '#555'
                        },
                        title: {
                            display: true,
                            text: 'Date',
                            color: '#443e8b',
                            font: {
                                family: "'Segoe UI', sans-serif",
                                size: 14,
                                weight: 'bold'
                            }
                        }
                    }
                },
                plugins: {
                    title: {
                        display: false
                    },
                    legend: {
                        display: true,
                        position: 'top',
                        labels: {
                            font: {
                                family: "'Segoe UI', sans-serif",
                                size: 13
                            },
                            color: '#443e8b',
                            boxWidth: 15,
                            usePointStyle: true,
                            pointStyle: 'circle'
                        }
                    },
                    tooltip: {
                        backgroundColor: 'rgba(74, 59, 179, 0.9)',
                        titleFont: {
                            family: "'Segoe UI', sans-serif",
                            size: 14
                        },
                        bodyFont: {
                            family: "'Segoe UI', sans-serif",
                            size: 13
                        },
                        padding: 12,
                        cornerRadius: 8,
                        displayColors: false,
                        callbacks: {
                            label: function(context) {
                                return 'Activity: ' + context.parsed.y;
                            }
                        }
                    }
                }
            }
        });
    </script>
</body>
</html>
"@

# Create a temporary HTML file
$htmlFilePath = "c:\temp\CABlockedSignInReport.html"
$htmlContent | Out-File -FilePath $htmlFilePath -Encoding utf8

# Open the HTML file in the default browser
Invoke-Item $htmlFilePath

Monitoring blocked sign-ins is a good start

It’s a good idea to get a point-in-time view of blocked sign-in data regularly, which PowerShell can help with, but it certainly isn’t the end goal. Only being able to monitor the past 30 days of data prevents you from being able to see trends over historic but significant periods, such as previous holidays or recurring events. 

The 30-day data limit is understandable; this way, it utilises existing data without having to create additional storage on the backend. However, to get trends over a longer period, this report’s data should be stored and ‘stitched’ together over a longer period. This could be done by sending the data to a Log Analytics Workspace or other similar service.

Daniel Bradley

Daniel Bradley is a UK-based technical architect and Microsoft MVP in Security. He specialises in Microsoft Entra, Intune, and Microsoft Graph PowerShell and is the author of Microsoft Graph PowerShell for Administrators. He writes and edits Our Cloud Network using experience gained from working with Microsoft cloud environments.

Leave a Reply