Microsoft 365: Kullanıcıların Son Giriş Tarihini Bulun


Gereksinim: Microsoft 365 kiracısındaki tüm kullanıcıların son Oturum Açma Tarihini ve saat damgasını bulun.

PowerShell, Office 365'te Son Giriş Tarihi Raporunu Oluşturacak

Kullanıcı hesaplarını etkili bir şekilde yönetmek, Office 365 kullanan kuruluşlar için çok önemlidir. Kullanıcı yönetiminin temel yönlerinden biri, son oturum açma tarihleri de dahil olmak üzere kullanıcı etkinliklerini izlemektir. Bu blog yazımızda PowerShell kullanarak Office 365'teki kullanıcıların son giriş tarihini bulma sürecinde size yol göstereceğiz.

Microsoft 365 Admin Center, Azure AD Oturum Açma Günlükleri ve PowerShell cmdlet'i Get-AzureADAuditSignInLogs - Üçünün de Office 365'te 30 günlük günlük kaydı sınırlaması vardır!

PowerShell kullanan tüm kullanıcıların son 30 gün içindeki son giriş tarihini araştırmak istiyorsanız İşte size uygun script:


#Config Parameters
$CSVPath = "C:\Temp\SigninLogs.csv"
$SigninLogs = @()

#Connect to Azure AD
Connect-AzureAD | Out-Null

#Get All users from Azure AD
$AllUsers = Get-AzureADUser -All $true
$TotalUsers = $AllUsers.Count

#Function to get the last login time stamp of the user
Function Get-UserLastLogin([string] $UserObjectID)
{
    Try {
        Write-host -f Yellow "Collecting Last Login date of the User Object:"$UserObjectID

        #Get the Successful Signin Logs of the user
        $SigninLog = Get-AzureADAuditSignInLogs -All:$true -Filter "userID eq '$UserObjectID' and status/errorCode eq 0" | Select -First 1

        #Return Last Login Date
        $LoginDetails = New-Object psobject -Property @{
            LoginTime = $SigninLog.CreatedDateTime
            App = $SigninLog.AppDisplayName
        }
        Return $LoginDetails
    }
    Catch {
        $message = $_
        If ($message -like "*Too Many Requests*")
        {
            Write-host "`tSleeping for 10 seconds due to throttling limitations..." -ForegroundColor Cyan
            Sleep 10
            #Recursive function call to retry the entry that was throttled
            Get-UserLastLogin $UserObjectID
        }
        Else
        {
            Write-host $Message -ForegroundColor Red           
        }
    }
}

#Loop through all users
$Counter = 1
$AllUsers | ForEach-Object {
    Write-Progress -Activity "Checking Signin Logs:" -Status "Processing $($_.UserPrincipalName) ($Counter of $TotalUsers)" -PercentComplete (($Counter / $TotalUsers) * 100) 
    #Call the function to get the sign-in log
    $SignInLog = Get-UserLastLogin $_.ObjectID
    
    #Get the Last Login Date
    If ($SignInLog.LoginTime -eq $Null)
    {
        $LastLoginDate = "No Log-in Events Found!"
    }
    Else
    {
        $LastLoginDate = Get-Date $SignInLog.LoginTime
    }

    #Collect data
    $SigninLogs += [PSCustomObject][ordered]@{
        UserLoginName   = $_.UserPrincipalName
        UserDisplayName   = $_.DisplayName
        LastLogin  = $LastLoginDate
        Application = $SignInLog.App
    }
    $Counter++
}

$SigninLogs
#Export Data to CSV
$SigninLogs | Export-Csv -Path $CSVPath -NoTypeInformation

Ancak yukarıdaki komut dosyası yalnızca 30 gün boyunca veri alabilir.

Graph API PowerShell'i kullanarak Office 365 için Son Giriş Raporu Oluşturun

Microsoft Graph API, son oturum açma tarihini 30 günlük bir sınır olmadan alabilir!

Önkoşullar

Office 365 kullanıcısının son giriş bilgilerine erişmeden önce Microsoft Graph PowerShell modülünü yüklemeniz gerekmektedir. Microsoft Graph PowerShell Modülü Nasıl Kurulur? için buraya bakın.


#Connect to Microsoft Graph
Connect-MgGraph -Scopes "AuditLog.Read.All","User.Read.All"

#Set the Graph Profile
Select-MgProfile beta

#Properties to Retrieve
$Properties = @(
    'Id','DisplayName','Mail','UserPrincipalName','UserType', 'AccountEnabled', 'SignInActivity'    
)

#Get All users along with the properties
$AllUsers = Get-MgUser -All -Property $Properties #| Select-Object $Properties

$SigninLogs = @()
ForEach ($User in $AllUsers)
{
    $SigninLogs += [PSCustomObject][ordered]@{
            LoginName       = $User.UserPrincipalName
            Email           = $User.Mail
            DisplayName     = $User.DisplayName
            UserType        = $User.UserType
            AccountEnabled  = $User.AccountEnabled
            LastSignIn      = $User.SignInActivity.LastSignInDateTime
    }
}

$SigninLogs

#Export Data to CSV
$SigninLogs | Export-Csv -Path "C:\Temp\SigninLogs.csv" -NoTypeInformation

Bu komut dosyası, Office 365 kiracınızdaki tüm kullanıcıları alır, son oturum açma bilgilerini toplar ve sonuçları bir CSV raporuna gönderir.

İlgili makaleler