ค้นหาเว็บไซต์

Microsoft 365: ค้นหาวันที่เข้าสู่ระบบล่าสุดของผู้ใช้


ข้อกำหนด: ค้นหาวันที่และเวลาเข้าสู่ระบบล่าสุดของผู้ใช้ทั้งหมดในผู้เช่า Microsoft 365

PowerShell เพื่อสร้างรายงานวันที่เข้าสู่ระบบล่าสุดใน Office 365

การจัดการบัญชีผู้ใช้อย่างมีประสิทธิภาพเป็นสิ่งสำคัญสำหรับองค์กรที่ใช้ Office 365 หนึ่งในสิ่งสำคัญของการจัดการผู้ใช้คือการตรวจสอบกิจกรรมของผู้ใช้ รวมถึงวันที่เข้าสู่ระบบครั้งล่าสุด ในโพสต์บนบล็อกนี้ เราจะแนะนำคุณตลอดกระบวนการค้นหาวันที่เข้าสู่ระบบล่าสุดของผู้ใช้ใน Office 365 โดยใช้ PowerShell

ศูนย์การจัดการ Microsoft 365, บันทึกการลงชื่อเข้าใช้ Azure AD และ PowerShell cmdlet Get-AzureADAuditSignInLogs - ทั้งสามมีข้อจำกัดบันทึก 30 วันใน Office 365!

หากคุณต้องการค้นหาวันที่เข้าสู่ระบบล่าสุดภายใน 30 วันที่ผ่านมาของผู้ใช้ทั้งหมดที่ใช้ PowerShell นี่คือสคริปต์สำหรับคุณ:


#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

อย่างไรก็ตาม สคริปต์ข้างต้นสามารถรับข้อมูลได้เพียง 30 วันเท่านั้น

สร้างรายงานการเข้าสู่ระบบล่าสุดสำหรับ Office 365 โดยใช้ Graph API PowerShell

Microsoft Graph API สามารถรับวันที่เข้าสู่ระบบครั้งล่าสุดโดยไม่มีขีดจำกัด 30 วัน!

ข้อกำหนดเบื้องต้น

ก่อนที่จะเข้าถึงข้อมูลการเข้าสู่ระบบครั้งล่าสุดของผู้ใช้ Office 365 คุณต้องติดตั้งโมดูล Microsoft Graph PowerShell ดูวิธีการติดตั้ง Microsoft Graph PowerShell Module ได้ที่นี่


#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

สคริปต์นี้จะดึงข้อมูลผู้ใช้ทั้งหมดในผู้เช่า Office 365 ของคุณ รวบรวมข้อมูลการลงชื่อเข้าใช้ล่าสุดของพวกเขา และส่งออกผลลัพธ์ไปยังรายงาน CSV

บทความที่เกี่ยวข้อง