Replies: 1 comment
-
|
And if looking up several users at the same time is wanted or needed the code below can be used the following way:
<#
This function requires you to connecto to Graph with Connect-MgGraph -Scopes "User.Read.All, AuditLog.Read.All"
#>
function Get-LastSuccessfulSignin {
param (
$user
)
# Check Connection To Graph
$context = Get-MgContext
if ([string]::IsNullOrEmpty($context)) {
Write-Error "Please connect to Microsoft Graph.."
break
}
if ($user -gt 1) {
foreach ($mu in $user) {
# User lookup
try {
$u = Get-MgUser -UserId $mu -ErrorAction Stop
}
catch {
Write-Error "Failed to get a user with UPN/ID: $($mu)"
}
# Getting users last successfull logon
$uri = "https://graph.microsoft.com/beta/users/$($u.Id)?select=signInActivity"
try {
$result = Invoke-MgGraphRequest -Method GET -Uri $uri -ErrorAction Stop
Write-Output "Last successful sign in from user $($u.UserPrincipalName) `n $($result.signInActivity.lastSuccessfulSignInDateTime) `n Last successful non interactive sign in `n $($result.signInActivity.lastNonInteractiveSignInDateTime)"
}
catch {
Write-Error "Failed to get last successful sign in for user $($u.UserPrincipalName)"
}
}
}
else {
# User lookup
try {
$u = Get-MgUser -UserId $user -ErrorAction Stop
}
catch {
Write-Error "Failed to get a user with UPN/ID: $($user)"
break
}
# Getting users last successfull logon
$uri = "https://graph.microsoft.com/beta/users/$($u.Id)?select=signInActivity"
try {
$result = Invoke-MgGraphRequest -Method GET -Uri $uri -ErrorAction Stop
Write-Output "Last successful sign in from user $($u.UserPrincipalName) `n $($result.signInActivity.lastSuccessfulSignInDateTime) `n Last successful non interactive sign in `n $($result.signInActivity.lastNonInteractiveSignInDateTime)"
}
catch {
Write-Error "Failed to get last successful sign in for user $($u.UserPrincipalName)"
}
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
With the new Graph functionality to check for the last successful sign in of users without browsing the sign in logs in the Entra portal I decided to create a small function to make it easier. It currently supports lookup with UPN or the Entra Object ID of the user.
Beta Was this translation helpful? Give feedback.
All reactions