In a previous post we went through configuring and connecting to Microsoft Graph API.
In this post we will going through querying sign-in logs.
Connecting to Microsoft GraphAPI Using PowerShell – TheSleepyAdmins
We have been trying to audit guest account activity and sign-in logs are the only way I have been able to find if these account’s have been active for the last 30 days. Instead of manually filtering sign-in logs from Azure AD I want to automate this using Graph.
To query sign-in logs the below API permission are required. since we are using client secret we only require Application permission.
Below is the link to the Microsoft doc I used for getting info on listing sign-ins.
List signIns – Microsoft Graph v1.0 | Microsoft Docs
Permission type | Permissions (from least to most privileged) |
---|---|
Delegated (work or school account) | AuditLog.Read.All and Directory.Read.All |
Delegated (personal Microsoft account) | Not supported |
Application | AuditLog.Read.All and Directory.Read.All |
Next step was to run the command to get to the access token for connecting to Microsoft Graph this is covered in the previous post so we won’t be going over that here.
To connect to the sign-in Graph use the below Url
https://graph.microsoft.com/v1.0/auditLogs/signIns
Below is the command to connect and view all sign-in logs data
$LoginUrl = "https://graph.microsoft.com/v1.0/auditLogs/signIns"
(Invoke-RestMethod -Headers @{Authorization = "Bearer $($token)"} -Uri $LoginUrl -Method Get).value
This will return all the default values for each sign-in log.

We only wanted to have Displayname,UPN,IP,App used and date the log was created. We also wanted to only have Logs that where created in the last 30 days.
$LoginUrl = "https://graph.microsoft.com/v1.0/auditLogs/signIns"(Invoke-RestMethod -Headers @{Authorization = "Bearer $($token)"} -Uri $LoginUrl -Method Get).value | Select-Object userDisplayName,userPrincipalName,ipAddress,clientAppUsed,createdDateTime | Where-Object {$_.userPrincipalName -notlike "*DomainName.com" -and $_.createdDateTime -gt "2020-09-29"}
Below is the results from the above query.

Now that we have the query, we can either run the query manually or in my case I will be setting up a script to run in a scheduled task to export this data.