Share via

PowerShell - User - Get Last Connection Date

Anonymous
2022-10-24T01:33:11+00:00

Hello,

I would like to get this date for each Office 365 user (only when the value is more than X days ago):

I tried to use the Get-AzureADAuditSignInLogs function but I am getting timeouts even if I add try/catch,... the problem is already known.

Any ideas?

Regards

Microsoft 365 and Office | Install, redeem, activate | For business | Other

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

2 answers

Sort by: Most helpful
  1. Anonymous
    2022-10-24T18:33:43+00:00

    Hello,

    Thanks to the reply: I agree with you that the Get-mailboxstatistics could be the best option but the thing is that we have some users that just have access to office 365 apps, that is why we want to base on AzureAD.

    Here is the full function I am using:

    Function GetLastSignInDate { param ( [string]$guestUserID ) $startTime = (get-date).AddDays(-90).ToString("yyyy-MM-dd") $filter = "startsWith(userId,'" + $guestUserID + "')" Try { $Result = Get-AzureADAuditSignInLogs -Filter $filter -Top 1 | Select-Object CreatedDateTime, UserPrincipalName if ($Result.CreatedDateTime -lt $startTime) { #$output = ($Result.UserPrincipalName.ToString() + "," + $Result.CreatedDateTime.ToString()) #Write-host $output -ForegroundColor Green return $Result } } Catch { $message = $_ if ($message -like "*Too Many Requests*") { Write-host "Sleeping for 10 seconds due to throttling limitations..." -ForegroundColor Yellow sleep 10 #Nested function call to retry the entry that was throttled GetLastSignInDate $guestUserID } elseif ($message -like "*This request is throttled*") { Write-host "Sleeping for 10 seconds due to throttling limitations..." -ForegroundColor Yellow sleep 10 #Nested function call to retry the entry that was throttled GetLastSignInDate $guestUserID } elseif ($message -like "*null-valued*") { $output = ($guestUserID + ", Not Found") Write-host $output -ForegroundColor Gray } elseif ($message -like "*Invalid filter clause*") { $output = ($guestUserID + ", Invalid character") Write-host $output -ForegroundColor Gray } elseif ($message -like "*Error reading JToken*") { $output = ($guestUserID + ", Script stopped due to authentication token timeout") Write-host $output -ForegroundColor White -BackgroundColor Red exit } else { $output = ($guestUserID + ",Error - " + $message.ToString().SubString(0,15)) Write-host $output -ForegroundColor Red } }}

    I tried to optimize the script but I am getting timeouts almost I increase the start date time. Indeed, if I search for all users that connected withing 2 days, it is working fine.

    Regards

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2022-10-24T12:07:16+00:00

    Dear Yann M_98800,

    Good day! Thank you for posting to Microsoft Community. We are happy to help you.

    I understand that you have a query on getting the Last connection date for all mailboxes

    Thank you for your time and efforts on troubleshooting this from your end. I kindly want to collect more details on the scenario you are encountering to help us isolate the concern.

    Kindly share with us the information below:

    "I tried to use the Get-AzureADAuditSignInLogs function", Could you kindly share with us screenshots or the exact errors you encountered running the command.

    Meanwhile I reproduced and managed to export the last sign in log to a .csv file using below commands:

    **Kindly note the following while running the command:

    -Please replace C:\Users\Administrator\Desktop with the location where you want to export the csv

    -When the command is running and generating the results, you will see some errors for any mailboxes which are unlicensed. Please kindly ignore them until the process is complete and then review csv results.

    If you may have any other questions or concerns on this feel free to let me know and I will be happy to help.

    We look forward to your response. Thanks for your cooperation.

    Sincerely,

    Anesu | Microsoft Community Moderator

    Was this answer helpful?

    0 comments No comments