Is there a way in AzureAD to get last logins and what program they logged in with?

Anthony 21 Reputation points
2022-10-28T18:58:26.563+00:00

Hello,
I am trying to get the last login of the users in my tenant as well as what service (Outlook / Sharepoint / etc) they used to log in.
I've ran a few scripts that give me results but nothing that I really need.

I am looking for a script (or a set of cmdlets) that would output me a CSV that would look something like this

Column A = UPN
Column B = Last Login
Column C = What they used to log in (Outlook / Sharepoint / Teams)

I'm wondering if there is a way directly in Azure AD to do this or if it is something I would need to run in Powershell?
Thank you very much in advance!

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,389 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
19,669 questions
0 comments No comments
{count} votes

8 answers

Sort by: Most helpful
  1. Anthony 21 Reputation points
    2022-11-01T20:29:04.177+00:00

    Hello, I got past the AzureAD error
    But now I am getting this error

    Get-AzureADAuditSignInLogs : Error occurred while executing GetAuditSignInLogs  
    Code: UnknownError  
    Message: Too Many Requests  
    InnerError:  
      RequestId: 224647ce-a92e-46c4-b20a-525835e42c82  
      DateTimeStamp: Tue, 01 Nov 2022 19:41:14 GMT  
    HttpStatusCode: 429  
    HttpStatusDescription:  
    

    My script looks like this (am I doing something wrong)?

    $file = Import-Csv -Path "TEST-UPN-LIST.csv" -delimiter ","  
      
    foreach ($user in $file) {  
        Get-AzureADAuditSignInLogs -Filter "UserPrincipalName eq '$User'" -Top 1 | `Export-csv -path .\Test.csv -Append -Encoding UTF8  
        select CreatedDateTime, UserPrincipalName, IsInteractive, AppDisplayName, IpAddress, TokenIssuerType, @{Name = 'DeviceOS'; Expression = {$_.DeviceDetail.OperatingSystem}}  
               
    }  
    

    Does this look right? Why would I be getting too many requests?
    Is there any way around this?

    0 comments No comments

  2. Anthony 21 Reputation points
    2022-11-02T00:37:33.923+00:00

    Anyone have any ideas?
    I'm at a bit of a loss.
    If I'm going about this all wrong I'm open to any other suggestion / way of doing this

    Basically, going back to my original post all I really want is to be able to have a script that will look at my CSV file that has my users in column A under the header UserPrincipalName and output it to a CSV file that has

    Column A = Username
    Column B = Last sign in date
    Column C = What application they used

    I'm really in need of some help here

    0 comments No comments

  3. Akshay-MSFT 16,126 Reputation points Microsoft Employee
    2022-11-03T10:22:40.283+00:00

    Hello @Anthony ,

    I was able to dig through this and found desired results. Kindly try the following script:

    CLS; $StartDate = (Get-Date).AddDays(-2); $StartDate = Get-Date($StartDate) -format yyyy-MM-dd
    Write-Host "Fetching data from Azure Active Directory..."
    $Records = Get-AzureADAuditSignInLogs -Filter "createdDateTime gt $StartDate" -all:$True
    $Report = [System.Collections.Generic.List[Object]]::new()
    ForEach ($Rec in $Records) {
    Switch ($Rec.Status.ErrorCode) {
    "0" {$Status = "Success"}
    default {$Status = $Rec.Status.FailureReason}
    }
    $ReportLine = [PSCustomObject] @{
    TimeStamp = Get-Date($Rec.CreatedDateTime) -format g
    User = $Rec.UserPrincipalName
    Name = $Rec.UserDisplayName
    IPAddress = $Rec.IpAddress
    ClientApp = $Rec.ClientAppUsed
    Device = $Rec.DeviceDetail.OperatingSystem
    Location = $Rec.Location.City + ", " + $Rec.Location.State + ", " + $Rec.Location.CountryOrRegion
    Appname = $Rec.AppDisplayName
    Resource = $Rec.ResourceDisplayName
    Status = $Status
    Correlation = $Rec.CorrelationId
    Interactive = $Rec.IsInteractive }
    $Report.Add($ReportLine) }
    Write-Host $Report.Count "sign-in audit records processed."
    $Report| Format-Table -AutoSize
    $Report |Export-Csv -Path 'SignReport2.csv'

    The output in PS console would look like this:

    256735-image.png

    The output in Excel would look like this:

    256723-image.png

    Please do let me know if you have any queries in the comments section.

    Thanks,
    Akshay Kaushik

    Please "Accept the answer" and "Upvote" if the suggestion works as per your business need. This will help us and others in the community as well.