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

ค้นหาเวลาเข้าสู่ระบบล่าสุดของผู้ใช้ SharePoint จาก AD ด้วย PowerShell


ข้อกำหนด: ค้นหาเวลาเข้าสู่ระบบล่าสุดของผู้ใช้ SharePoint ทั้งหมดของฟาร์มเพื่อค้นหาผู้ใช้ที่ไม่ได้ใช้งาน

วิธีแก้ไข: เราสามารถดึงข้อมูลเวลาเข้าสู่ระบบล่าสุดของผู้ใช้จาก Active Directory (หากผู้ให้บริการการรับรองความถูกต้องคือ AD) โดยใช้ PowerShell ในโพสต์บนบล็อกนี้ เราจะแสดงวิธีค้นหาเวลาเข้าสู่ระบบล่าสุดของผู้ใช้ SharePoint


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
Import-Module ActiveDirectory

$UserAccount="vani"
$User = Get-ADUser -Filter {SamAccountName -like $UserAccount} | Get-ADObject -Properties lastLogon 
  
$LogonTime=[DateTime]::FromFileTime($User.LastLogon)
Write-host $LogonTime

มาเพิ่มการจัดการข้อผิดพลาดเพิ่มเติมและค้นหาเวลาเข้าสู่ระบบล่าสุดของผู้ใช้ SharePoint ทั้งหมด

สคริปต์ PowerShell เพื่อค้นหาผู้ใช้ SharePoint เข้าสู่ระบบครั้งล่าสุด:

ลองใช้โมดูล SharePoint และ Active Directory PowerShell ร่วมกันเพื่อค้นหาเวลาเข้าสู่ระบบล่าสุดของผู้ใช้ทั้งหมด


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
Import-Module ActiveDirectory

#Custom Function Get the Last Logon Time of the User from AD 
Function Get-ADUserLastLoginTime($UserAccount)
{
    Try {
        #Set the Error Action
        $ErrorActionPreference = "Stop"

        $User = Get-ADUser -Filter {SamAccountName -like $UserAccount} | Get-ADObject -Properties lastLogon 
  
           if($user.LastLogon) 
           {
               return([DateTime]::FromFileTime($User.LastLogon))
           }
           else
           {
             return "Not Found!"
           }
        }
    catch {
          Write-Host $_.Exception.Message -ForegroundColor Red
        }
    finally {
          #Reset the Error Action to Default
          $ErrorActionPreference = "Continue"
        }
}
#Get All User Profiles
$SiteURL="https://intranet.crescent.com"
$ServiceContext  = Get-SPServiceContext -site $SiteURL
$UserProfileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($ServiceContext)
#Get All User Profiles
$UserProfilesColl = $UserProfileManager.GetEnumerator()

Foreach($UserProfile in $UserProfilesColl)
{
    $UserAccount=$UserProfile["UserName"]
    $LogonTime = Get-ADUserLastLoginTime -UserAccount $UserAccount
    Write-host "Last Logon Time of the User $UserAccount is $LogonTime"
}

โปรดทราบว่า SharePoint จะไม่จัดเก็บการประทับเวลาการเข้าสู่ระบบครั้งล่าสุด สคริปต์ด้านบนดึงข้อมูลจาก Active Directory ดังนั้น ข้อมูลอาจไม่สอดคล้องกันเล็กน้อย เพราะหากผู้ใช้เข้าสู่ระบบอื่น ๆ เช่น Exchange Server Email AD จะมีการประทับเวลานั้น ไม่สำคัญว่าผู้ใช้จะเข้าสู่ระบบ SharePoint หรือไม่! นี่คือโพสต์อื่นของฉันเกี่ยวกับวิธีรับการประทับเวลาการลงชื่อเข้าใช้ครั้งล่าสุดของผู้ใช้จาก Azure AD สำหรับ SharePoint Online

อย่างไรก็ตาม หากคุณแยกวิเคราะห์บันทึก IIS ของแอปพลิเคชันเว็บ SharePoint โดยใช้ Log parser คุณสามารถค้นหากิจกรรมของผู้ใช้เฉพาะได้: วิธีใช้ Log Parser กับ SharePoint

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