Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##############################################################################
- #Powershell 2,3
- #Script to find the last login time of a user by comparing last login times
- # recorded on each domain controler and selecting the last one.
- #
- ##############################################################################
- # by: ki01s
- # Date: 7/3/13
- #
- ##############################################################################
- #Import the module to access Active Directory
- Import-Module ActiveDirectory
- #ask for a username to look up
- $LoginName = Read-Host "What user would you like to look up?"
- #Create function to use in other scripts
- function Get-ADUserLastLogon([string]$userName)
- {
- #get full list of domain controllers in current domain
- $dcs = Get-ADDomainController -Filter {Name -like "*"}
- $time = 0
- #Query each domain controler to find the last date and time user logged in.
- foreach($dc in $dcs)
- {
- $hostname = $dc.HostName
- $user = Get-ADUser $userName | Get-ADObject -Properties lastLogon
- #compare times from each DC to determine last login time
- if($user.LastLogon -gt $time)
- {
- $time = $user.LastLogon
- }
- }
- $dt = [DateTime]::FromFileTime($time)
- #display the name and time of last login.
- Write-Host $username "last logged on at:" $dt }
- Get-ADUserLastLogon -UserName $LoginName
- ##Unhash the two lines below, if running script from command line to pause script and wait for keypress before closing window.
- #Write-Host "Press any key to continue ..."
- #$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Advertisement
Add Comment
Please, Sign In to add comment