this script connects to AD and grabs the users you specify depending upon the LDAP path you provide
does retreives the correct lastlogon time in date time format and outputs a CSV with one column for the display name and other for lastlogontime
lastlogontime : is when the user last logged to the domain
# ==============================================================================================
#
# Script Name: get User and last logon time into a CSV file
#
# AUTHOR: Mohamed Garrana ,
# DATE : 4/13/2010
#
# COMMENT:
#
# ==============================================================================================
function connect{
$ADpath = "LDAP://OU=Users,OU=IT Department,DC=domain,DC=win2k,DC=dom" #set your ldap path to your domain or certian OU
$searcher = New-Object DirectoryServices.DirectorySearcher
$RootSearch = New-Object directoryservices.directoryentry $ADpath
$searcher.searchroot = $RootSearch
$searcher.filter = "(objectClass=user)"
$allusers = $searcher.findall()
foreach ($user in $allusers) { get-lastlogontime }
}
function get-lastlogontime {
BEGIN { }
PROCESS {
#Write-Host $user.Properties.Displayname[0]
try {
$name = $user.Properties.displayname[0]
$adlastlogon=$user.Properties.lastlogon[0]
}
Catch {
Write-Host -ForegroundColor Red " <<< WHoops ... >>> $name : Error reading a required property from the AD User object, execution will continue anyway ;)"
continue
}
finally {
[datetime]$initialdate="01/01/1601" #microsoft date used to calculate lastlogon
$lastlogon = $initialdate.Addseconds(($adlastlogon*1e-7)) #nano seconds interval + initial date
$AdUser = New-Object psobject
$AdUser | Add-Member NoteProperty DisplayName ($name)
$AdUser | Add-Member NoteProperty LastLogon ($lastlogon)
Write-Output $AdUser
}
}
END{}
}
$csvfile="C:\test\userlastlogon.csv" #set the location of your output file
connect | Export-Csv $csvfile
# ==============================================================================================
#
# Script Name: get User and last logon time into a CSV file
#
# AUTHOR: Mohamed Garrana ,
# DATE : 4/13/2010
#
# COMMENT:
#
# ==============================================================================================
function connect{
$ADpath = "LDAP://OU=Users,OU=IT Department,DC=domain,DC=win2k,DC=dom" #set your ldap path to your domain or certian OU
$searcher = New-Object DirectoryServices.DirectorySearcher
$RootSearch = New-Object directoryservices.directoryentry $ADpath
$searcher.searchroot = $RootSearch
$searcher.filter = "(objectClass=user)"
$allusers = $searcher.findall()
foreach ($user in $allusers) { get-lastlogontime }
}
function get-lastlogontime {
BEGIN { }
PROCESS {
#Write-Host $user.Properties.Displayname[0]
try {
$name = $user.Properties.displayname[0]
$adlastlogon=$user.Properties.lastlogon[0]
}
Catch {
Write-Host -ForegroundColor Red " <<< WHoops ... >>> $name : Error reading a required property from the AD User object, execution will continue anyway ;)"
continue
}
finally {
[datetime]$initialdate="01/01/1601" #microsoft date used to calculate lastlogon
$lastlogon = $initialdate.Addseconds(($adlastlogon*1e-7)) #nano seconds interval + initial date
$AdUser = New-Object psobject
$AdUser | Add-Member NoteProperty DisplayName ($name)
$AdUser | Add-Member NoteProperty LastLogon ($lastlogon)
Write-Output $AdUser
}
}
END{}
}
$csvfile="C:\test\userlastlogon.csv" #set the location of your output file
connect | Export-Csv $csvfile