我必须编写一个简单的脚本,列出在特定 OU 中的每台计算机上登录的所有用户。
我已经尝试过这个函数Get-UserLogon -OU '[distinguished name]'
(见这里),但它没有返回任何标准输出。
有什么选择吗?
我必须编写一个简单的脚本,列出在特定 OU 中的每台计算机上登录的所有用户。
我已经尝试过这个函数Get-UserLogon -OU '[distinguished name]'
(见这里),但它没有返回任何标准输出。
有什么选择吗?
您是否尝试过为您的用户提供上次登录时间和日期:
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
有一种替代方法不会迭代域中的所有计算机,但它依赖于所有用户将其主目录重定向到网络共享。
如果您的域中存在这种情况,请尝试:
# 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 中列出的每台计算机,然后向它们查询用户登录列表。正如预期的那样,这些计算机中的大多数都拒绝连接(可能它们已关闭或只是脱机)。
还有其他方法可以检索此类信息吗?域控制器是否以这种集中方式存储登录信息?
我在这里找到了这种可能的解决方案(在线程的底部阅读),但我对 VBscript 一点也不熟悉,我想在 PowerShell 中实现这段代码。