如何列出特定计算机上的登录信息?

问题描述

我必须编写一个简单的脚本来列出在特定 OU 中每台计算机上登录的所有用户

我尝试过函数 Get-Userlogon -OU '[distinguished name]'(请参阅 here),但它没有返回任何标准输出

有其他选择吗?

解决方法

您是否尝试过这样向您提供用户上次登录的时间和日期:

Get-ADUser -Filter * -SearchBase "ou=users,dc=contoso,dc=local" -ResultPageSize 0 -Prop CN,lastLogonTimestamp | Select CN,@{n="lastLogonDate";e={[datetime]::FromFileTime($_.lastLogonTimestamp)}} | Export-CSV -NoType last.csv

参考:https://expert-advice.org/active-directory/powershell-script-to-get-lastlogon-timestamp-for-specific-ou-and-export-to-csv-file/

,

有一种替代方法,它不会迭代域中的所有计算机,它依赖于所有用户将其主目录重定向到网络共享。

如果您的域是这种情况,请尝试:

# the UNC \\Server\Share name of the network share where all user homedirectories are
$usersHomePath = '\\HomesServer\HomesShare$' 

# split this UNC path to get the server name and share name in separate variables
$server,$share = $usersHomePath.TrimStart("\") -split '\\'


# get an array of SamAccountNames for all users in the OU
$users = (Get-ADUser -Filter * -SearchBase '[distinguished name]').SamAccountName

$result = Get-CimInstance -ClassName Win32_ServerConnection -ComputerName $server | 
            Where-Object { $_.ShareName -eq $share -and $users -contains $_.UserName } |
            Select-Object @{Name = "SamAccountName"; Expression = { $_.UserName }},@{Name = "ComputerName"; Expression = {(([System.Net.Dns]::GetHostEntry($_.ComputerName).HostName) -split "\.")[0]}}

#output in console
$result

# output to Csv
$result | Export-Csv -Path 'UsersOnComputers.csv' -NoTypeInformation
,

我试图简单地调试 Get-UserLogon 函数。

此函数尝试连接到 OU 中列出的每台计算机,然后向它们查询用户登录列表。正如预期的那样,这些计算机中的大多数拒绝连接(可能它们已关闭或只是离线)。

还有其他方法可以检索此类信息吗?域控制器是否以这种集中方式存储登录信息?

,

我找到了这种可能的解决方案 here(在线程底部阅读),但我对 VBscript 一点也不熟悉,我想在 PowerShell 中实现此代码。