Skip to Content
Mastering Active Directory - Second Edition
book

Mastering Active Directory - Second Edition

by Dishan Francis
August 2019
Intermediate to advanced
786 pages
20h 22m
English
Packt Publishing
Content preview from Mastering Active Directory - Second Edition

Last logon time

On certain occasions, we are required to find when a user successfully logs on to a domain. This can be for audit purposes or for troubleshooting purposes:

$username = Read-Host 'What is the User account you looking for ?'   $dcs = Get-ADDomainController -Filter {Name -like "*"}      foreach($dc in $dcs)   {      $hostname = $dc.HostName     $user = Get-ADUser $userName -Server $hostname -Properties lastLogon     $lngexpires = $user.lastLogon     if (-not ($lngexpires)) {$lngexpires = 0 }     If (($lngexpires -eq 0) -or ($lngexpires -gt [DateTime]::MaxValue.Ticks))     {       $LastLogon = "User Never Logged In"     }      Else     {       $Date = [DateTime]$lngexpires       $LastLogon = $Date.AddYears(1600).ToLocalTime()     }  }  Write-Host $username "last logged on at:" $LastLogon

The preceding ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Mastering Active Directory - Third Edition

Mastering Active Directory - Third Edition

Dishan Francis
Active Directory, 5th Edition

Active Directory, 5th Edition

Brian Desmond, Joe Richards, Robbie Allen, Alistair G. Lowe-Norris

Publisher Resources

ISBN: 9781789800203Supplemental Content