使用 Powershell 获取上次登录时间、计算机和用户名

使用 Powershell 获取上次登录时间、计算机和用户名

我有一个脚本,可以获取域中每台计算机的最后登录时间。

我的脚本:

$dcs = Get-ADComputer -Filter { OperatingSystem -NotLike '*Server*' } -Properties OperatingSystem

foreach($dc in $dcs) { 
    Get-ADComputer $dc.Name -Properties lastlogontimestamp | 
    Select-Object @{n="Computer";e={$_.Name}}, @{Name="Lastlogon"; Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp)}}
}

==================================

结果:

计算机上次登录
-------- ---------
DC1 2013 年 6 月 6 日 16:38:24
DC2 2013 年 6 月 6 日 16:30:40

=============================================

我还想获取谁/哪个帐户进行了此登录。例如:

计算机上次登录用户
-------- ------------------ ----
DC1 2013 年 6 月 6 日 16:38:24 用户 2
DC2 2013 年 6 月 6 日 16:30:40 用户 1

答案1

这个可能并不完美,但它能让你走上正轨。要获取确切的最后一个用户,请参阅此脚本。它将为您提供有关如何过滤最后一个确切用户的更多信息。在下面的示例中,我使用了 select-object -First 1,这应该可以很好地指示最后一个登录的用户。要获取最后一个登录的用户,您需要使用

Get-WmiObject -Class Win32_UserProfile

为了“连接” Get-ADComputer 和 Get-WMIObject 信息,我使用了哈希表。

如果您从域管理员帐户运行此程序,则可以删除 -credential $credential 部分。否则,请保留它,您可以从安装了 RSAT 工具的普通工作站运行它,这样 Get-ADComputer 就可用了。

代码如下:

$computers = Get-ADComputer -Filter { OperatingSystem -NotLike '*Server*' } -Properties OperatingSystem
$credential = Get-Credential -Message "Please enter your administrator username and password"

foreach($computer in $computers) { 
    $pcinfo = Get-ADComputer $computer.Name -Properties lastlogontimestamp | ` 
              Select-Object @{Name="Computer";Expression={$_.Name}}, ` 
             @{Name="Lastlogon";Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp)}}

    $lastuserlogoninfo = Get-WmiObject -Class Win32_UserProfile -ComputerName $computer.name -Credential $credential | Select-Object -First 1
    $SecIdentifier = New-Object System.Security.Principal.SecurityIdentifier($lastuserlogoninfo.SID)
    $username = $SecIdentifier.Translate([System.Security.Principal.NTAccount])

    # Create hashtable for properties
    $properties = @{'Computer'=$pcinfo.Computer;
                    'LastLogon'=$pcinfo.Lastlogon;
                    'User'=$username.value
                   } #end $properties
    write-output (New-Object -Typename PSObject -Property $properties)
}

使用时请检查格式。有些内容我必须添加转义符(`)才能适合脚本窗口。

谢谢,蒂姆。

答案2

并非要打破每个人的幻想,但 Win32_UserProfile 下的“LastUseTime”字段不是上次登录;它只是“配置文件”更新的最后时间戳(无论出于何种原因)。您会发现所有配置文件都会因各种原因定期更新——安装程序、策略更新等。

此外,数据默认是无序的,因此“-First 1”参数是无用的,除非数据首先按“LastUseTime”排序——这同样由于上述原因而毫无价值。

我很遗憾地说上述脚本没有为“上次登录”提供任何有用的数据。

答案3

这是 Tim 的代码,我对其进行了更新以显示计算机 IP 地址和操作系统并导出数据。

 $computers = Get-ADComputer -Filter { OperatingSystem -NotLike '*Server*'} -Properties OperatingSystem

 foreach($computer in $computers) { 
     $pcinfo = Get-ADComputer $computer.Name -Properties ipv4Address, OperatingSystem ,lastlogontimestamp | 
               Select-Object @{n="Computer";e={$_.Name}}, 
              @{Name="Lastlogon";Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp)}},ipv4Address,OperatingSystem

     $lastuserlogoninfo = Get-WmiObject -Class Win32_UserProfile -ComputerName $computer.name -Credential $credential | Select-Object -First 1
     $SecIdentifier = New-Object System.Security.Principal.SecurityIdentifier($lastuserlogoninfo.SID)
     $username = $SecIdentifier.Translate([System.Security.Principal.NTAccount])

     # Create hashtable for properties
     $properties = @{'Computer'=$pcinfo.Computer;
                     'LastLogon'=$pcinfo.Lastlogon;
                     'ipv4Address'=$pcinfo.ipv4Address;
                    'OperatingSystem'=$pcinfo.OperatingSystem
                     'User'=$username.value
                    } #end $properties
     write-output (New-Object -Typename PSObject -Property $properties) |
     export-csv .\Computers.csv -append -notypeinformation -encoding "unicode"         
            }

Powershell Get-ADComputer 属性表达式

相关内容