TransWikia.com

How to get users login history in SharePoint OnPrem?

SharePoint Asked on December 18, 2020

Is there any way to get users Login History in SharePoint on-prem 2016? Kindly Suggest.

2 Answers

User below Powershell to get users from SharePoint.

This script will help save us developers a lot of time in getting all the users from an individual or group. So, here is the script.

  1. Copy the code below to a .ps1 file.
  2. Run the .ps1 file on the SharePoint PowerShell modules.
  3. You don't need to do any update on the script.

Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue
$Currentime = get-date -format "yyyyMMdd_hhmmtt" $filename = "FarmUsers" $datafile = ("{0}{1}.csv" -f $filename, $Currentime)
$headerfile = "type,user,group,weburl,webtitle" $headerfile | out-file -FilePath $datafile
$iissitedata = get-spwebapplication foreach($farmsite in $iissitedata) {
foreach ($SiteCollection in $farmsite.sites)
{
write-host $SiteCollection -foregroundcolor Blue
foreach ($web in $SiteCollection.Allwebs)
{
write-host " " $web.url $web.name "users:" -foregroundcolor yellow
foreach ($usersite in $web.users)
{
write-host " " $usersite -foregroundcolor white
$data = ("RootUser,{0},-,{1},{2}" -f $usersite, $web.url,$web.name)
$data | out-file -FilePath $datafile -append
}
foreach ($group in $web.Groups)
{
Write-host " " $web.url $group.name: -foregroundcolor green
foreach ($user in $group.users)
{
Write-host " " $user -foregroundcolor white
$data = ("GroupUser,{0},{1},{2},{3}" -f $user, $group, $web.url, $web.name)
$data | out-file -FilePath $datafile -append
}
}
$web.Dispose()
}
} }

Get All Users In A SharePoint 2010/13/16 Farm Using PowerShell Script

Then try following code with loop by passing each user in below code to get last logon from AD.

$dcs = Get-ADDomainController -Filter {Name -like "*"}
$time = 0
foreach($dc in $dcs)
{ 
  $hostname = $dc.HostName
  $user = Get-ADUser $User.UserLogin.ToString().split('\')[1] | Get-ADObject -Properties lastLogon 
  if($user.LastLogon -gt $time) 
  {
    $time = $user.LastLogon
  }
}
$dt = [DateTime]::FromFileTime($time)
$UserData | Add-Member -type NoteProperty -name "last logged on at" -value $dt

Here $User is the SharePoint User which we found from SharePoint with domain name in it.

Answered by Hardik on December 18, 2020

Per my knowledge, there is no OOB solution to get users Login History in SharePoint 2016. However, you can develop a custom auditing solution. You'll need to add custom code to pages which involve login action(recording the login's in your page's code. U)

You can use cookie to be a flag for checking whether there is an user just log in. After the page loaded, if the cookie is null, it suggests that there is an user just log in our site, then we can get this user’s information using JavaScript Object Model and add the information into a custom list. With this list, we can monitor user’s login details. See the reply provided by Dennis from below article:

https://social.technet.microsoft.com/Forums/en-US/0cd4d531-cb61-4d90-aa70-413267f4a735/how-to-know-login-and-logout-details-of-a-user-in-sharepoint-online-2013?forum=sharepointdevelopment

In addition, below post has a similar issue with you, which may be helpful to you:

https://social.msdn.microsoft.com/Forums/office/en-US/38c631e5-8158-482c-8332-5b6193098471/check-which-users-login-a-sharepoint-site-during-the-past-30-days-and-whats-the-last-login-time?forum=sharepointdevelopmentprevious

Answered by imp. MSFT on December 18, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP