Use JSON Batching to Fetch Entra ID Audit Sign-In Logs

JSON Batching is a Fast and Efficient Way to Retrieve Large Quantities of Entra ID Audit Sign-in Logs

A Reddit discussion about fetching Entra audit sign-in logs with the Microsoft Graph API underlined a simple fact: Microsoft 365 tenants can generate a very large number of sign-in events. The Get-MgAuditLogSignIn cmdlet from the Microsoft Graph PowerShell SDK is an excellent way to fetch sign-in logs. Usually, the cmdlet is used with a filter to restrict the number of logs that need to be fetched, such as this example of checking whether the Entra ID accounts associated with shared mailboxes have been used to sign in (the accounts should be disabled).

The Get-MgAuditLogSignIn cmdlet supports the –All parameter to paginate automatically and fetch all available logs. Even for a small Entra ID Free tenant, where sign-in logs are retained for only seven days, this can take time. The potential volume is much larger in tenants with Entra ID P1 or P2 licenses, which are also required to download sign-in logs through the Graph API.

In this article, I discuss how to approach the problem of how to handle large quantities of sign-in logs by using a divide and conquer technique to generate a series of PowerShell commands, each fetching a slice of data, before combining the information. That technique works, but maybe a better answer exists.

Divide and Conquer with JSON Batching

JSON batching is a good candidate whenever large quantities of data need to be fetched from Graph endpoints. Instead of relying on a single sequence of paginated requests, processing is divided into batches containing up to 20 individual requests. Each request retrieves a slice of the required data, and the results are combined to create the full set of sign-in records. JSON batching operates at the HTTP level, so the script must explicitly handle pagination and transient errors for individual requests within each batch.

One approach is to divide the period for which the audit data is required into individual nonoverlapping time windows and to generate Graph API requests with filters to find audit sign-in events for those time windows as the basis for batched requests. Depending on the period used, there might be one or more batches created for processing. A PowerShell queue seems like a good mechanism to manage the requests.

Each request fetches up to 1,000 sign-in logs, the maximum and default page size for the API. The script checks each response for an @odata.nextLink value and, when one exists, queues another request to retrieve the next page. A request looks like this (the time-based filter is encoded):

Id        : 30
Method    : GET
Url       : /auditLogs/signIns?$filter=createdDateTime%20ge%202026-11-02T13%3A59%3A22.075Z%20and%20createdDateTime%20lt%202026-11-03T10%3A23%3A01.510Z&$top=1000
Retry     : 0
NotBefore : 01/01/0001 00:00:00

JSON batching doesn’t avoid Graph throttling and the volume of requests submitted in quick succession can cause the Graph to throttle individual requests temporarily. An HTTP 429 (Too Many Requests) response signals that the script should back off and retry the request after the interval specified by the Retry-After response header. As a further step to prevent throttling, the script also pauses briefly between each batch submission.

A Script to Use JSON Batching to Download Entra Audit Sign-In Logs

My best effort at coding up a script to do the job is available in the Microsoft 365 for IT Pros GitHub repository. After fetching the complete set of audit sign-in records, the script deduplicates the set (just in case) before generating either an Excel worksheet or CSV file containing the data (depending on whether the ImportExcel module is available). Feel free to improve the code by submitting a pull request!

Microsoft says that the Graph API isn’t designed for extracting very large quantities of activity data because of potential pagination and performance issues. This script doesn’t remove those limitations. Instead, it uses time-based filters and batching to make retrieval more efficient for volumes that can reasonably be processed through the API.

The script supports both delegated and app-only authentication (the AuditLog.Read.All permission is the only one that’s needed). In an app-only session or if you use the code as an Azure Automation runbook, make sure to change the location for the output file.

Some Odd Sign-In Logs

Inevitably, when testing something like this script, you pay attention to the output data to make sure that it’s what you expect. When checking the set of audit sign-in logs, I noticed a set of failure events recorded for my account, including several blocked by Entra because they originated from IP addresses known for malicious traffic (Figure 1).

Entra Audit sign-in records with failure statuses for various reasons.

JSON batching.
Figure 1: Audit sign-in records with failure statuses for various reasons

The sign-in attempts came from around the world. Only one was from Dublin, where I live, and that was after I updated my tenant to allow passwords to be changed without knowledge of the original password. As I noted in the article on this topic, Teams complained afterwards and forced me to sign out and back in again to continue. That’s the failed sign-in with the 650053 code (The application ‘[Name]’ asked for scope ‘[Scope]’ that doesn’t exist on the resource ‘[Resource]’).

The malicious IP addresses are owned by Amazon Web Services, Oracle, and TOR exit nodes operated by organizations like the Foundation for Applied Privacy in Vienna, Austria. Threat can come from anywhere, and keeping a close eye on audit sign-in logs helps tenant administrators to understand which accounts attackers are attempting to compromise.


Need help to write and manage PowerShell scripts for Microsoft 365, including Azure Automation runbooks? Get a copy of the Automating Microsoft 365 with PowerShell eBook, available standalone or as part of the Microsoft 365 for IT Pros eBook bundle.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.