简体   繁体   中英

Last logon script

I would like to know how can I check the last login in my domain. I need to know how many days the user isn't logged in (Some variable that will be like a counter). for example, CSV file that will be with this variables:

USER- XXX 
Last Log in- DATA  
days logoff- **counter.

As a small base script to start with you could use something like this:

$lastlogon = @()
Get-ADUser -Filter * -Property LastLogonTimestamp | Select SamAccountName,LastLogonTimestamp | Foreach($_) { 
    $item = New-Object System.Object
    $item | Add-Member -type NoteProperty -Name Username -value $_.SamAccountName
    $item | Add-Member -type NoteProperty -Name LastLogon -value $([datetime]::FromFileTime($_.LastLogonTimestamp))
    $diff = New-TimeSpan -Start ([datetime]::FromFileTime($_.LastLogonTimestamp)) -End $(Get-Date) 
    $item | Add-Member -type NoteProperty -Name DaysSince -value $diff.Days
    $lastlogon += $item
}
$lastlogon | Export-Csv -Path C:\Temp\LastLogon.csv -NoTypeInformation -Delimiter ";" -Encoding UTF8

This will get the LastLogonTimestamp attribute of all your domain users, convert it to a more human readable format and then write it to the CSV file C:\\Temp\\LastLogon.csv you could open with eg Excel. Additionally it computes the difference between now and the LastLogonTimestamp and writes it to the column DaysSince using the `New-TimeSpan' cmdlet.

You may want to narrow your search down by having a look at Get-AdUser -Filter or -SearchBase .

Please be aware that there might be strange time values for users that never logged in, because this script does not check for valid or invalid timestamps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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