Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sitecore Powershell: Find users last login?

Tags:

I'm writing a very simple Sitecore PowerShell script to report on all users in our system. We'd like to know the last login date for each user, but I've had no luck getting at it. I can access $_.Profile.LastActivityDate, but that doesn't help as it appears it's the same value for all users. Any ideas what the expression is to access the last login date, or how I can find it? Thanks.

Get-User -Filter * |

Show-ListView -Property @{Label="User"; Expression={ $_.Profile.UserName} },
    @{Label="Full Name"; Expression={ $_.Profile.FullName} },
    @{Label="Email"; Expression={ $_.Profile.Email} },
    @{Label="Logged In"; Expression={ $_.Profile.LastActivityDate } }

Close-Window
like image 265
Derek Sheppard Avatar asked Jul 12 '16 23:07

Derek Sheppard


1 Answers

Get-User will output one or more Sitecore.Security.Accounts.User, whose Profile property is a Sitecore.Security.UserProfile which inherits from System.Web.Profile.ProfileBase. So, the LastActivityDate property should be the same as you'd get by accessing the profile outside of Sitecore.

That said, there is a chance that simply accessing the profile data will update the last activity date.

The LastActivityDate for a user is updated by the classes in the System.Web.Profile and the System.Web.UI.WebControls.WebParts namespaces whenever user data is retrieved from or set at the data source. ...

So, you may be able to avoid accessing the profile, and instead, retrieve MembershipUsers. MembershipUser has a property named LastLoginDate which I think is what you're after:

[System.Web.Security.Membership]::GetAllUsers() |

Show-ListView -Property @{Label="User"; Expression={ $_.UserName} },
    @{Label="Is Online"; Expression={ $_.IsOnline} },
    @{Label="Creation Date"; Expression={ $_.CreationDate} },
    @{Label="Last Login Date"; Expression={ $_.LastLoginDate} },
    @{Label="Last Activity Date"; Expression={ $_.LastActivityDate } }

If you need access to the profile without updating the last activity date, you might also try:

[System.Web.Security.Membership]::GetUser("sitecore\admin", $false)
like image 159
Derek Hunziker Avatar answered Sep 28 '22 03:09

Derek Hunziker