简体   繁体   English

如何扫描计算机列表并查找上次登录的用户以及该用户登录的日期

[英]How to scan a list of computers and find the last logged in user and the date this user logged in

I have the following script from a previous stackoverflow question.It works in that the script successfully scans my list of computers to report back my userid logged in for each computer. 我有一个来自上一个stackoverflow问题的以下脚本。它的工作原理是该脚本成功扫描了我的计算机列表,以报告每台计算机登录的用户ID。

But what I'm looking for is the last logged in user for each computer in my list. 但是我要寻找的是列表中每台计算机的最后一个登录用户。 I don't want to scan Active Directory as I don't have access to do that. 我不想扫描Active Directory,因为我无权执行此操作。 Here is the code I found that is getting me partway there: 这是我发现的让我感到困惑的代码:

    Get-Content -Path c:\ListOfMachines.txt | % {
  Get-WmiObject -ComputerName $_ -Namespace root\cimv2 -Class Win32_ComputerSystem -erroraction silentlycontinue | % {
    "$($_.Name): $($_.username)"
  }
}

Like I said it works in that it scans each computer in my list but I want it to report back the last logged in user and the date they logged in. Is this something someone has seen before? 就像我说的那样,它可以扫描列表中的每台计算机,但我希望它报告上次登录的用户及其登录日期。这是某人以前见过的东西吗?

Thank you. 谢谢。

You can grab the user SID with the most recent LastUseTime in the Win32_UserProfile class and assign it to a $User variable: 您可以在Win32_UserProfile类中获取具有最新LastUseTime的用户SID ,并将其分配给$ User变量:

$last_user= Get-WmiObject -ComputerName $_ -Namespace root\cimv2 -Class Win32_UserProfile | Sort-Object -Property LastUseTime -Descending | Select-Object -First 1 
$UserSID = New-Object System.Security.Principal.SecurityIdentifier($last_user.sid) 
$User = $UserSID.Translate([System.Security.Principal.NTAccount])

To format the date from the $last_user: 要格式化$ last_user中的日期,请执行以下操作:

[Management.ManagementDateTimeConverter]::ToDateTime($last_user.LastUseTime)

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

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