简体   繁体   English

获取每个用户配置文件的上次登录时间

[英]Get the Last Logon Time for Each User Profile

I need to get the last logon time for each user profile on a remote computer (not the local accounts).我需要获取远程计算机(不是本地帐户)上每个用户配置文件的最后登录时间。 I've tried something similar to the following using Win32_UserProfile & LastLogonTime, however this is not giving accurate results.我已经使用 Win32_UserProfile & LastLogonTime 尝试了类似于以下内容的操作,但这并没有给出准确的结果。 For example, one this computer, only 1 account has been used in the past year, however LastUpdateTime is showing very recent dates.例如,这台计算机在过去一年中只使用了一个帐户,但是 LastUpdateTime 显示的是最近的日期。 Some accounts have not even been logged into and should say "N/A", but it doesn't.有些帐户甚至没有登录,应该说“N/A”,但事实并非如此。

 $RemoteSB_UserADID = Get-WmiObject win32_userprofile -Property * | Where-Object {$_.LocalPath -like "*users*"} | Sort-Object $_.LastUseTime | ForEach-Object{
     $Parts = $_.LocalPath.Split("\")
     $ADID = $Parts[$Parts.Length - 1]
                        
     if ($ADID -ne "SPECIALPURPOSEACCOUNT1" -and $ADID -ne "SPECIALPURPOSEACCOUNT2"){
         $Time = $null
         try{
             $Time = $_.ConvertToDateTime($_.LastUseTime)
         }catch{
             $Time = "N/A"
         }
                            
         "[$ADID | $Time]"
     }
}

Example Output示例 Output

[Acct1 | [帐户 1 | 03/13/2022 07:18:19] 03/13/2022 07:18:19]
[Acct2 | [帐户 2 | 03/15/202214:59:16] 03/15/202214:59:16]
[Acct3 | [帐户 3 | 03/13/2022 07:18:19] 03/13/2022 07:18:19]
[Acct4 | [账户 4 | 03/16/2022 11:53:17] <--- only "active" account 03/16/2022 11:53:17] <--- 只有“活跃”账户

How can I go about retrieving accurate (or decently accurate) login times for each user profile? go 如何为每个用户配置文件检索准确(或相当准确)的登录时间? Thanks!谢谢!

It would help to know for what reason you need that, so that I know how to find a (better) solution for you.知道您出于什么原因需要它会有所帮助,这样我就知道如何为您找到(更好的)解决方案。

If you need to cleanup your profiles not used for a long time at the target system, then take the last changed date of "ntuser.dat".如果您需要清理您在目标系统上长时间未使用的配置文件,请获取“ntuser.dat”的最后更改日期。 That is the last logon if you define logon like logging on to a new session. If the user was logged on and simply locked the computer or used standby and then relogs then this date won't change.如果您将登录定义为登录到新的 session,则这是最后一次登录。如果用户已登录并只是锁定计算机或使用待机状态,然后重新登录,则此日期不会更改。

Use this to get this date from all users you have access to but possibly not getting real user names使用它从您有权访问但可能无法获得真实用户名的所有用户那里获取此日期

Get-ChildItem \\REMOTECOMPUTERNAMEHERE\Users\*\ntuser.dat -Attributes Hidden,Archive | Select @{Name="NameByFolder";Expression={($_.DirectoryName -split "\\")[-1]}},LastWriteTime

Or this a bit more complex version或者这个有点复杂的版本

Invoke-Command -ComputerName REMOTECOMPUTERNAMEHERE -ScriptBlock {$UsersWithProfilePath = @{}
dir "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" |
where {$_.name -like "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\S-1-5-21-*"} |
foreach {$UsersWithProfilePath[([System.Security.Principal.SecurityIdentifier]$_.name.split("\")[-1]).Translate( [System.Security.Principal.NTAccount]).Value] = $_.GetValue("ProfileImagePath")}
foreach ($Name in $UsersWithProfilePath.Keys) {@{$Name =(dir (join-path $UsersWithProfilePath.$Name ntuser.dat) -Attributes Hidden,Archive,System).LastWriteTime}}}

Depending on what you need you need to change it a bit.根据您的需要,您需要对其进行一些更改。 Sorry for the long codelines... it is late here.抱歉,代码行太长了……来晚了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM