如何跟踪AD用户上次访问/映射共享文件夹的日期和时间

问题描述 投票:0回答:1

我试图获取Windows Server中定义的所有用户的列表,其中包含上次登录到服务器或映射/访问文件共享的日期时间。为此,我尝试了以下脚本:

Get-ADUser -Filter * -SearchBase "dc=whatever,dc=local" -ResultPageSize 0 -Prop CN,samaccountname,LastLogonTimestamp | Select CN,samaccountname,@{N='Last‌Logon'; E={[DateTime]::FromFileTime($_.LastLogonTimestamp)}}

这为我提供了以下输出:

CN                             samaccountname                Last‌Logon                  
--                             --------------                -----------                  
Administrador                  Administrador                 08/02/2019 17:30:00          
Invitado                       Invitado                      01/01/1601 1:00:00           
krbtgt                         krbtgt                        01/01/1601 1:00:00           
Maite                          maite                         01/01/1601 1:00:00           
Mari Carmen                    mcarmen                       01/01/1601 1:00:00           
Emilio                         emilio                        01/01/1601 1:00:00           
Erica                          erica                         01/01/1601 1:00:00 

我知道管理员以外的用户已访问服务器文件共享(其中一些每天将它们映射为驱动器),但列表中唯一有效的登录日期是Adminstrator。

看起来此命令仅在用户通过RDP或控制台实际登录服务器时进行报告。

每次用户访问任何服务器共享时,如何获得类似的查询报告?

powershell windows-server-2012
1个回答
0
投票

您的文件共享访问权限会创建一个网络登录(登录类型3),它不会影响LastLogon属性(分别为LastLogonTimestamp)。在这种情况下,您必须从域控制器事件日志中获取信息,其中记录所有登录。以下脚本将输出其登录类型为最后24h的任何用户的上次登录日期。

#86400000 = 24h
$FilterXPath = '*[System[EventID=4624 and TimeCreated[timediff(@SystemTime) <= 86400000]]]' 
$LogonEvents = Get-WinEvent -LogName Security -FilterXPath $FilterXPath

$Logons = foreach ($LogonEvent in $LogonEvents) {
    [PSCustomObject]@{
        LogonDate = $LogonEvent.TimeCreated
        UserName = $LogonEvent.Properties[5].Value
        UserDomain = $LogonEvent.Properties[6].Value
        LogonType = $LogonEvent.Properties[8].Value
    }
}

$LogonsGroupedByUserName = $Logons | Group-Object -Property UserName

foreach ($Group in $LogonsGroupedByUserName) {     
    $Group.Group | 
        Sort-Object -Property LogonDate -Descending | 
        Select-Object -First 1
}
© www.soinside.com 2019 - 2024. All rights reserved.