Hello, i need help on a powershell script. I have this script where it gets the lastlogon attribute of users on al DC, and outputs the latest, however, there are some users that have the lastlogontimestamp as the latest.
I would like to modify the script below that it will query for noth lastlogon and lastlogontimestamp and outputs the latest.
=====================================
function Get-ADUsersLastLogon()
{
$dcs = Get-ADDomainController -Filter {Name -like "*"}
$OUpath = '<OUPath>'
$users = Get-ADUser -Filter {Enabled -eq $true} -SearchBase $OUpath
$time = 0
$exportFilePath = "c:\tmp\lastLogontest.csv"
$columns = "Name,LastLogon"
Out-File -filepath $exportFilePath -force -InputObject $columns
foreach($user in $users)
{
foreach($dc in $dcs)
{
$hostname = $dc.HostName
$currentUser = Get-ADUser $user.SamAccountName | Get-ADObject -Server $hostname -Properties lastLogon
if($currentUser.LastLogon -gt $time)
{
$time = $currentUser.LastLogon
}
}
$dt = [DateTime]::FromFileTime($time).ToString('dd/MM/yyyy hh:mm:ss tt')
$row = $user.Name+","+$dt
Out-File -filepath $exportFilePath -append -noclobber -InputObject $row
$time = 0
}
}
Get-ADUsersLastLogon
Apprciate yourr kind help,
Fraismikki